从c#调用mysql存储过程?

时间:2013-03-21 10:36:35

标签: mysql c#-4.0 stored-procedures

我试图从我的c#应用程序调用一个mysql存储过程,该过程有3个参数,我动态传递,问题是我不是错误也不是输出,只是我得到空数据

 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; 

2 个答案:

答案 0 :(得分:2)

我认为这个例子可以帮助你

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;
            }

或访问此页面并使用相同的信息http://www.java2s.com/Code/CSharp/Database-ADO.net/ModifyDataTableinsertdatatodatabasetable.htm

答案 1 :(得分:0)

尝试此代码

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();