如何使用 Transaction 来使 SQL SERVER 进行 COMMIT 或 ROLLBACK

walter_lee68 2005-12-19 11:01:53
我要执行以下2个 SQL 语句,成功后才 COMMIT
1.UPDATE TABLE1 SET FIELD1="AAA" WHERE ...
2.UPDATE TABLE2 SET FIELD1="AAA" WHERE ...


在 C# 中的写法是怎样的:

Conn1.Open();
SqlCommand CM1=new SqlCommand("Begin Transcation", Conn1);
:
:
CM1.ExecuteScalar();
Conn1.Close();

请帮忙指出

...全文
404 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
diablo1003 2006-03-13
  • 打赏
  • 举报
回复
mark
alexlimingjun 2005-12-19
  • 打赏
  • 举报
回复
public void RunSqlTransaction(string myConnString)
{
SqlConnection myConnection = new SqlConnection(myConnString);
myConnection.Open();

SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction myTrans;

// Start a local transaction
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback("SampleTransaction");
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}

Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
}
MSND里的
seaonce 2005-12-19
  • 打赏
  • 举报
回复
using (SqlConnection Conn = new SqlConnection(数据库连接字符串))
{
int upd1 = 0;
int upd2 = 0;
Conn.Open();
SqlTransaction trans = Conn.BeginTransaction();
using (SqlCommand Cmd = new SqlCommand())
{
Cmd.CommandText = "update....";
upd1 = Cmd.ExecuteNonQuery();
Cmd.CommandText = "update ....";
upd2 = Cmd.ExecuteNonQuery();
Cmd.Dispose();
}
if( upd1==1 && upd2==1)
{
trans.Commit();
}
else
{
trans.Rollback();
}

Conn.Dispose();
Conn.Close();
}
nate_liu 2005-12-19
  • 打赏
  • 举报
回复
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
myConnection.Open();

// Start a local transaction.
SqlTransaction myTrans = myConnection.BeginTransaction();

// Enlist the command in the current transaction.
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}

Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}

請參閱
charles_y 2005-12-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/charles_y/archive/2005/12/13/550833.aspx

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧