The use method of Transation
2006-09-14 11:13
429 查看
在.NET框架中,我们可以使用SqlTransaction类定义一个事务。此后,我们就可以使用commit或rollback函数控制事务。当然了,我们也可以使用.NET框架提供的异常处理功能获取系统异常。下面是一个有关的例子:
[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来定义事务。
[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来定义事务。
相关文章推荐
- Understanding the Use of invertedSet method of NSCharacterSet
- The use method of afxDump and TRACE
- #146 – Use GetValueSource Method to Find the Source of a Dependency Property Value
- Use the command of tar to multi-part archive method.
- C Language Study - the use of symbol '##'
- iOS 初始化报错 cannot assign to self outside of a method in the init
- RYU 启动出现unsupported version 0x1. If possible, set the switch to use one of the versions[4]
- 关于error:Cannot assign to 'self' outside of a method in the init family
- Use MFC in a Static Library,This may be due to a corruption of the heap....
- The resource could not be loaded because the App Transport Security policy requires the use of a sec
- The method of type must override a superclass method解决方式
- Your *.gwt.xml module configuration prohibits the use of the current doucment rendering mode
- URL validation failed. The error could have been caused through the use of the browser's navigation
- The class demostrates the use of java.io.RandomAccessFile
- AFNetworking 提示"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" 解决办法
- What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
- 轻松解决 MyEclipse、Eclipse 编译时提示 @Override The method of type must override a superclass method 即 @Overr
- Is the onDestroy method of Activity certain to call?
- Get the data of today only use c language