OleDbConnection

本文介绍了一个使用OleDbConnection和OleDbTransaction进行数据库操作的例子。通过本地事务管理一系列插入操作,并演示了如何在出现异常时回滚事务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

string connectString;
// ...
// ...
using (OleDbConnection connection = new OleDbConnection(connectString))
{
    connection.Open();

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

    // Enlist the command in the current transaction.
    OleDbCommand command = connection.CreateCommand();
    command.Transaction = sqlTran;

    try
    {
        command.CommandText =
          "INSERT INTO Table1(Name) VALUES('Wrong size')";
        command.ExecuteNonQuery();

        command.CommandText =
          "INSERT INTO Table2(Name) VALUES('Wrong color')";
        command.ExecuteNonQuery();

        sqlTran.Commit();
        Console.WriteLine("Both records were written to database.");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine("Neither record was written to database.");
        try{sqlTran.Rollback();}catch{}
    }
}

posted on 2007-07-22 12:33 NetwordBoy 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/networdboy/archive/2007/07/22/827242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值