同时将记录插入2个不同的数据库

同时将记录插入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