C#2005 + ACCESS,如何在OleDbCommand执行多条语句?

jeefis 2007-08-31 04:28:02
如题。
Ado.net的System.Data.OleDb命名空间中有OleDbTransaction类,应该可以执行事务吧,但我在CommandText中存放两条语句就会出错,请问解决办法,别教我分两次执行,数据库不能改。
谢谢!
...全文
526 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeefis 2007-09-01
  • 打赏
  • 举报
回复
一楼的很好,多谢指教!
JGood 2007-08-31
  • 打赏
  • 举报
回复
Access只能一次执行一条语句。
可以使用OleDbTransaction(一楼的方法)来执行多条语句。
ivyorg 2007-08-31
  • 打赏
  • 举报
回复
OleDbCommand command = new OleDbCommand(selq1 + ";" + selq2, conn);
这样也行。
jeefis 2007-08-31
  • 打赏
  • 举报
回复
谢谢楼上,回去试下先
活靶子哥哥 2007-08-31
  • 打赏
  • 举报
回复
多条SQL语句是数据库级别的事务!!!

如下代码示例如何使用OleDbTransaction的(来自MSDN)
public void ExecuteTransaction(string connectionString)
{
using (OleDbConnection connection =
new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand();
OleDbTransaction transaction = null;

// Set the Connection to the new OleDbConnection.
command.Connection = connection;

// Open the connection and execute the transaction.
try
{
connection.Open();

// Start a local transaction
transaction = connection.BeginTransaction();

// Assign transaction object for a pending local transaction.
command.Connection = connection;
command.Transaction = transaction;

// Execute the commands.
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();

// Commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
// The connection is automatically closed when the
// code exits the using block.
}

62,046

社区成员

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

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

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

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