Más contenido relacionado
Más de Hiroki Takahashi (8)
C# Database操作4 トランザクション処理-
- 3. [INS/UPD/DEL文実行]の処理
前回作成した[INS/UPD/DEL文実行]ボタンのクリックイベントを、以下のように編集します。
private void btnExec_Click(object sender, EventArgs e)
{
using (var trans = _sqlConn.BeginTransaction()) -----①
{
try
{
SqlCommand command = _sqlConn.CreateCommand();
command.Transaction = trans; -----②
command.CommandText = txtSQL.Text;
int cnt = command.ExecuteNonQuery();
trans.Commit(); -----③
MessageBox.Show(cnt.ToString() + "件処理しました。");
}
catch (SqlException sqlex)
{
trans.Rollback(); -----④
MessageBox.Show(sqlex.Message);
}
}
}