同时将记录插入2个不同的数据库 [英] Inserting records into 2 different databases simultaneously

查看:103
本文介绍了同时将记录插入2个不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用C#开发Windows应用程序.
我正在从文本文件导入记录,并将这些记录插入数据库表中.
我的问题是我想一次在2个不同的数据库中插入记录.
为此,我需要建立2个不同的连接.
我知道SqlTransactions可以在1个以上的表中插入记录,因此如果出现故障,整个操作将回滚.
是否有类似可用的东西可用于在2个不同的数据库中插入记录,例如如果某件事失败,整个操作将被回滚.

谢谢,
Nagendra.

Hi All,

I am working on a windows application in C#.
I am importing records from a text file and inserting those records in database tables.
My problem is i want to insert records in 2 different databases at once.
For doing this, i need to establish 2 different connections.
I know about SqlTransactions to insert records in more then 1 tables, so if something fails, whole operation will be rollback.
Is there anything similar available which i can use to insert records in 2 different databases, such as if something fails whole operation will be rollback.

Thanks,
Nagendra.

推荐答案

使用存储过程.在一个存储过程中,您可以在多个数据库中插入数据,以防万一任何查询失败-回滚所有内容.例如:

Use stored procedures.Inside one stored procedure you can insert data in multiple databases and incase any of the query fails - rollback everything. For ex:

Create Procedure Test
As
Begin
  BEGIN TRY
    Begin Tran

   Insert into table1db1 ...


   Insert into NameOfDatabase1.dbo.table2db2 ...

    COMMIT TRAN
  END TRY
  BEGIN CATCH
    ROLLBACK TRAN
  END CATCH
END


这篇关于同时将记录插入2个不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆