SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=northwind");cn.Open();SqlTransaction ta = cn.BeginTransaction();
SqlCommand cmd = cn.CreateCommand();
cmd.Transaction = ta;try
{
//你的数据操作
ta.Commit();
Console.WriteLine("成功");
}
catch (Exception e)
{
try
{
ta.Rollback();
}
catch (SqlException ex)
{
if (ta.Connection != null)
{
Console.WriteLine(ex.GetType() + " 回滚事务");
}
}
Console.WriteLine(e.GetType());
Console.WriteLine("没有数据写入数据库");
}
finally
{
cn.Close();
}
SqlCommand cmd = cn.CreateCommand();
cmd.Transaction = ta;try
{
//你的数据操作
ta.Commit();
Console.WriteLine("成功");
}
catch (Exception e)
{
try
{
ta.Rollback();
}
catch (SqlException ex)
{
if (ta.Connection != null)
{
Console.WriteLine(ex.GetType() + " 回滚事务");
}
}
Console.WriteLine(e.GetType());
Console.WriteLine("没有数据写入数据库");
}
finally
{
cn.Close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货