-1

I have two SQL Server instances (LocalDb and LiveDb) using two different connection strings. I currently use C#'s SqlBulkCopy to transfer master tables to from the Livedb to my localDb. This works fine and is very fast.

However, I need to UPDATE tables on my LiveDb based on a query of records from my LocalDB. Is there an example of doing this in a C# Windows application?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jeff
  • 19
  • 1
  • Is LiveDB production database? – Amit Kumar Singh Sep 01 '17 at 17:52
  • Yes. My app is a time clock, check in/check out that I am writing. The program runs fully on the local instance of sql. It writes check in records to the live production db every 30 seconds. My issue is how to update a field on the live db when a local db check out happens? – Jeff Sep 01 '17 at 18:03
  • A little more info. The record I need to update on live db is based on the local db's record – Jeff Sep 01 '17 at 18:09

1 Answers1

1

Sounds like you just need to set up a linked server... here's a good note on that.

Once you do that, it will be something like this (run this using your LiveDb connection)

update l
   set l.someColumn = lo.someColumn
from
   TableName l                      --live db here...
inner join
   LocalDb.dbo.TableName lo on      --linked server
   lo.TableNameID = l.TableNameID   --or what ever join condition

Or, if you were doing some sort of staging process... perhaps something like this.

select
   ...
into #myTempTable          --often faster to stage data when pulling from linked server
from 
   LocalDb.dbo.TableName   --linked server

update l
   ...
TableName l                      --live db here...   
etc...
S3S
  • 24,809
  • 5
  • 26
  • 45