我在我的代码中使用了SqlTransaction,但遇到了一些问题。在搜索过程中,我看到很多人都在使用带有SqlTransaction的using语句。
使用这种类型的语句与SqlTransaction有什么好处和/或区别?
using (SqlConnection cn = new SqlConnection())
{
using (SqlTransaction tr = cn.BeginTransaction())
{
//some code
tr.Commit();
}
}
目前我的代码是这样的:
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["T3"]);
cn.Open();
SqlTransaction tr = cn.BeginTransaction();
try
{
//some code
tr.Commit();
cn.Close();
}
catch(Exception ex)
{
tr.Rollback();
cn.Close();
throw ex;
}
一个比另一个更有优势是什么?