从c#调用mysql StoredProcess?
我试图从我的c#应用程序调用一个mysql存储过程,该过程有3个参数,我动态传递,问题不是我出错,也不是输出,只是我得到的是空数据表从c#调用mysql StoredProcess?,mysql,c#-4.0,stored-procedures,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;
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;
我认为这个例子会对你有所帮助
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;
}
或者使用相同的信息访问此页面请尝试此代码
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();
你确定你的SP返回了数据库记录吗?@adityaa我试试。也适用于mysql。只需将SqlCommand、SqlDataAdapter等更改为MySqlCommand、MySqlDataAdapter。