adityaa
adityaa

Reputation: 111

calling mysql storedprocedure from c#?

im trying to call a mysql stored procedure from my c# application , the procedure has 3 parameters which im passing dynamically, the problem is im not error nor the output, just im getting empty datatable

 MySqlConnection con = new MySqlConnection(myConnectionString);
 con.Open();
 MySqlCommand cmd = new MySqlCommand("User_details", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("username", "aditya");
cmd.Parameters.AddWithValue("password", "123");
cmd.Parameters.AddWithValue("gender", "male");
 MySqlDataAdapter da= new MySqlDataAdapter(cmd);
 DataTable dt = new DataTable();
 da.Fill(dt);
 dataGridView2.DataSource = dt; 

Upvotes: 0

Views: 11560

Answers (2)

Anass Ziyat
Anass Ziyat

Reputation: 9

try this code

DataTable dt = new DataTable();
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlDataAdapter da =new MySqlDataAdapter(nom_fonction,connection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("id", MySqlDbType.Int64).Value = id;
da.Fill(dt);

connection.Close();

Upvotes: 0

Arbejdsglæde
Arbejdsglæde

Reputation: 14098

I think this example will help you

protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();
                DataTable dt = new DataTable();
                try
                {
                    cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
                    cmd.Parameters.Add(new SqlParameter("@EMPLOYEENO", employeeNo));
                    cmd.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.GetBaseException().ToString(), "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    cmd.Dispose();
                    pl.MySQLConn.Close();
                }
                return dt;
            }

or visit this page with the same information http://www.java2s.com/Code/CSharp/Database-ADO.net/ModifyDataTableinsertdatatodatabasetable.htm

Upvotes: 2

Related Questions