时间:2021-05-20
复制代码 代码如下:
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#中事务处理和非事务处理方法。分享给大家供大家参考。具体如下:C#代码如下:StringconnectionString=Configurati
本文实例讲述了SQLServer存储过程中编写事务处理的方法。分享给大家供大家参考,具体如下:SQLServer中数据库事务处理是相当有用的,鉴于很多SQL初学
本文所述为C#事务处理(ExecuteTransaction)的一个实例,包含了创建SqlTransaction对象并用SqlConnection对象的Begi
本文实例讲述了php下pdo的mysql事务处理用法。分享给大家供大家参考。具体分析如下:php+mysql事务处理的几个步骤:1.关闭自动提交2.开启事务处理
在数据库连接上创建事务处理对象,然后调用事务处理对象来提交事务或回滚事务。简单的代码:privatevoidbutton1_Click(objectsend