提交大量数据时候,怎么防止数据丢失? 提交大量数据时候,怎么防止数据丢失?怎么在没有提交成功时,使数据保存原样呢?请帮忙写一下,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用数据库事务Transaction具体情况看看帮助吧 使用事务,详细说明请参考msdn:SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");myConnection.Open();// 启动一个事务SqlTransaction myTrans = myConnection.BeginTransaction();// 为事务创建一个命令SqlCommand myCommand = new SqlCommand();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){myTrans.Rollback();//事务回滚Console.WriteLine(e.ToString());Console.WriteLine("Neither record was written to database.");}finally{myConnection.Close();} asp.net控件 Enabled=false 字体颜色不能修改嘛? ASP.net(开发语言VB)如何用相对路径连接Access数据库 几个关于xml的错误 我下载了个div+css模板,如何在vs2008里用?我是新手 谢谢 IIS为何修改不了默认页? 同方知网北京技术有限公司招聘 如何用键盘钩子屏蔽系统功能键 调用自己定义类的问题,无法取到函数 javascript如何 调整 table 列 的显示顺序 Bitmap对象在部署的ASP.NET页面中使用错误。。。 如何在postback后,控制datagrid的数据行的显示位置? 请问怎么得到访问当前页面的上一页?
Transaction具体情况看看帮助吧
myConnection.Open();
// 启动一个事务
SqlTransaction myTrans = myConnection.BeginTransaction();
// 为事务创建一个命令
SqlCommand myCommand = new SqlCommand();
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)
{
myTrans.Rollback();//事务回滚
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}