Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 24-Jan-11 18:36pm
v2
Comments
Dave Kreskowiak 25-Jan-11 0:36am    
Changed tag. Question has nothing to do with C#.

1 solution

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:

SQL
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900