| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 622 人关注过本帖
标题:数据回滚
只看楼主 加入收藏
suntao163
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-8-19
收藏
 问题点数:0 回复次数:3 
数据回滚
在对数据库多表操作时,怎么在其中一条语句不执行时,回滚我知道用事务,应该怎么写啊
搜索更多相关主题的帖子: 数据库 语句 事务 
2006-10-27 14:04
huangs415
Rank: 1
等 级:新手上路
帖 子:102
专家分:0
注 册:2006-10-14
收藏
得分:0 
  这个我也想知道啊,我自己的一个网站里面的标题不能在固定筐架里面动,郁闷 想知道怎么样搞一下
2006-10-27 14:36
メ冰枫ぱ雪
Rank: 1
等 级:新手上路
威 望:2
帖 子:326
专家分:0
注 册:2004-11-13
收藏
得分:0 
SqlConnection.BeginTrans(); 不知道有没写错

动态网页技术交流群:16449874 免费网络收藏夹:http:///Favorite
2006-10-28 11:22
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 

SqlTransaction sqlTransaction = sqlconn.BeginTransaction( IsolationLevel.Serializable);

....
这里处理你的数据库
if (条件)
{
sqlTransaction.Rollback();//事务回滚
}
else
{
sqlTransaction.Commit();//事务提交
}


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();
// 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();
}
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();
}
}


等待相逢
2006-10-28 13:17
快速回复:数据回滚
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015814 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved