努力从带有参数的存储过程填充数据网格
Struggling to populate datagrid from stored procedure with parameters
protected DataTable RetrieveAlumni2()
{
{
MySqlConnection con = new MySqlConnection("server=100.0.0.0;user id=id;password=pass;database=db;persistsecurityinfo=True");
MySqlCommand cmd = new MySqlCommand("get_alumni_by_city", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
string city = textBox1.Text;
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@city", SqlDbType.VarChar).Value = city;
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
return dt;
}
}
给出错误:
"input string was not in a correct format"
mysql 服务器中的城市是 varchar。任何帮助将不胜感激。
检查存储过程中@city 的类型。如果它与您投掷的内容不匹配,它将拒绝它。
你试过这个吗?
cmd.Parameters.AddWithValue("@city","" + city + "");
protected DataTable RetrieveAlumni2()
{
{
MySqlConnection con = new MySqlConnection("server=100.0.0.0;user id=id;password=pass;database=db;persistsecurityinfo=True");
MySqlCommand cmd = new MySqlCommand("get_alumni_by_city", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
string city = textBox1.Text;
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@city", SqlDbType.VarChar).Value = city;
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
return dt;
}
}
给出错误:
"input string was not in a correct format"
mysql 服务器中的城市是 varchar。任何帮助将不胜感激。
检查存储过程中@city 的类型。如果它与您投掷的内容不匹配,它将拒绝它。
你试过这个吗?
cmd.Parameters.AddWithValue("@city","" + city + "");