1.合理使用事务提交(commited)和回滚 (undo)保证数据库完整。
SqlConnection conn = ...;
conn.Open();
SqlCommand cmd = conn.CreateCommand();
SqlTransaction tran = conn.BeginTransaction();
cmd.Transaction = tran;
try
{
cmd.CommandText = "insert into a (a) vlalues ('a')";
cmd.ExecuteNonQuery();
...
cmd.CommandText = "insert into b (b) vlalues ('b')";
cmd.ExecuteNonQuery();
...
}
catch (SqlException)
{
tran.Rollback();
return;
}
finally
{
conn.Close();
}
tran.Commit();