开发者

Weird problem when calling MySQL stored procedure from C#

I have a button with a click event :

private void btnShow_Click(object sender, EventArgs e)
{
  MySqlConnection con = new MySqlConnection("SERVER=localhost;DATABASE=airdb;UID=root;PASSWORD=123456");
  con.Open();
  DataTable dt = new DataTable();
  MySqlCommand cm = new MySqlCommand("CALL GetClient()", con);
  MySqlDataAdapter da = new MySqlDataAdapter(cm);
  开发者_运维百科da.Fill(dt);
  con.Close();
  dataGridView1.DataSource = dt;
}

On the first click it fills the dataGridView1 with data retrieved from stored procedure "GetClient" without any problem. But when I click the button again it returns count of 0 from stored procedure and all data on dataGridView1 disappears. The third click it works well again and the fourth click it returns count of 0, the fifth click fine, the sixth click failed, ... (similar to the next click)

But when I change to my code to :

DataTable dt = MySqlHelper.ExecuteDataset("SERVER=localhost;DATABASE=airdb;UID=root;PASSWORD=123456", "CALL GetClient()").Tables[0];
dataGridView1.DataSource = dt;

It works well in all situations.

So I wonder if there is anything wrong with the first code.

stored procedure : select * from Client


Sun.

I think You put break points and check the flow, where exactly problem is happening. and even i suggest u to store commandType as Stored procedure

  MySqlCommand cm = new MySqlCommand("Your SP NAMe", con);
  cm.Parameters.AddWithValue("YourParameters","Value);
  cm.CommandType=CommandType.StoredProcedure ;

i hope ur code works with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜