ADO.NET问题

zf19921020 2012-03-29 08:18:52
我用sqlcommand对数据表进行修改,当时运行的时候是执行成功了,但是修改的操作无法更新到数据表,每次关闭vs之后数据就自动回滚操作了,设置数据库复制到输出目录为从不复制也不得行,怎么办啊?求高手!!
...全文
90 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
听可笑的承诺 2012-03-29
  • 打赏
  • 举报
回复
没提交事物?
Mlovecoding 2012-03-29
  • 打赏
  • 举报
回复
事务的原则是开启后就必须回滚或提交!!!不能开启后就不管了!!!
qiume 2012-03-29
  • 打赏
  • 举报
回复
捕捉异常,事务完成后要提交:

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);
}
}
}
}
quifar123 2012-03-29
  • 打赏
  • 举报
回复
同意楼上
bdmh 2012-03-29
  • 打赏
  • 举报
回复
检查是否有事务控制
anzhiqiang_touzi 2012-03-29
  • 打赏
  • 举报
回复
开了数据库事务,但是没提交事务

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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