上方廣告

2009年4月18日 星期六

取得一次下多筆SQL指令的影響筆數

//使用委派事件來取得多筆指令受影響列
private void button5_Click(object sender, EventArgs e)
{
  using (SqlConnection conn = new SqlConnection(strConnStr))
  {
    conn.Open();
    using (SqlCommand cmd = new SqlCommand())
    {
      //StatementCompleted事件只適用於SqlClient
      cmd.StatementCompleted += new StatementCompletedEventHandler(cmd_StatementCompleted);
      cmd.CommandType = CommandType.Text;
      cmd.Connection = conn;
      cmd.CommandText = "Insert into Test.dbo.預存程序 (No1) Values ('HHH');" +
                        "Update Test.dbo.預存程序 Set No1 = 'NA' Where No1='NA' or No1='HHH'";
      cmd.ExecuteNonQuery();
    }
  }
}

private void cmd_StatementCompleted(object sender, StatementCompletedEventArgs e)
{
  MessageBox.Show("影響資料筆數:" + e.RecordCount.ToString());
}