事务的用法

项目中操作数据库经常用到的,方便以后使用

try

{

System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection ("Data Source=SVCTAG-3FYD33X;Database=xxx;User ID=sa;Password=123456");

System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
cm.Connection = cnn;
cnn.Open();
System.Data.SqlClient.SqlTransaction trans = cnn.BeginTransaction(IsolationLevel.Serializable);
cm.Transaction = trans;//这句一定不能忘了加

 

cm.CommandText = "insert into .....";
cm.ExecuteNonQuery();

.........

cm.CommandText = "update into .....";
cm.ExecuteNonQuery();

..........

trans.Commit();//提交

}

catch

{

   trans.rollback();//出错回滚

}

 

你可能感兴趣的:(.net,数据库,insert,数据库)