当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET中的事务处理和异常处理(2)

 ASP.NET中的事务处理和异常处理(2)

点击次数:27 次 发布日期:2008-11-26 12:35:42 作者:源代码网
源代码网推荐      下面是一个有关的例子:
源代码网推荐  
源代码网推荐  [Web Applicaion in C#]
源代码网推荐  
源代码网推荐  SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
源代码网推荐  
源代码网推荐  myConnection.Open();
源代码网推荐  
源代码网推荐  SqlTransaction myTrans = myConnection.BeginTransaction(); //使用New新生成一个事务
源代码网推荐  
源代码网推荐  SqlCommand myCommand = new SqlCommand();
源代码网推荐  
源代码网推荐  myCommand.Transaction = myTrans;
源代码网推荐  
源代码网推荐  try
源代码网推荐  
源代码网推荐  {
源代码网推荐  
源代码网推荐  myCommand.CommandText = "Update Address set location="23 rain street" where userid="0001"";
源代码网推荐  
源代码网推荐  myCommand.ExecuteNonQuery();
源代码网推荐  
源代码网推荐  myTrans.Commit();
源代码网推荐  
源代码网推荐  Console.WriteLine("Record is udated.");
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  catch(Exception e)
源代码网推荐  
源代码网推荐  {
源代码网推荐  
源代码网推荐  myTrans.Rollback();
源代码网推荐  
源代码网推荐  Console.WriteLine(e.ToString());
源代码网推荐  
源代码网推荐  Console.WriteLine("Sorry, Record can not be updated.");
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  finally
源代码网推荐  
源代码网推荐  {
源代码网推荐  
源代码网推荐  myConnection.Close();
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐  需要注意的是,如果使用OleDb类而不是Sqlclient类来定义SQL命令和连接,我们就必须使用OleTransation来定义事务。
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华