0

i'm using this code:

SqlCommand cmd = new SqlCommand(" MERGE customermaster AS target USING Tamio.dbo.memberform AS source ON target.id = source.id WHEN MATCHED THEN UPDATE SET target.name = source.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);", con);

I want to choose from my local database.

I try this but it says incorrect syntax

SqlCommand cmd = new SqlCommand(" MERGE customermaster AS target USING [local]Tamio.dbo.memberform AS source ON target.id = source.id WHEN MATCHED THEN UPDATE SET target.name = source.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);", con);

Also this is my local connection

SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Tamio;Integrated Security=True");

and this is my client pc connection

SqlConnection conn = new SqlConnection(@"Data Source=192.168.1.101;Initial Catalog=Imatismos;user ID=admin;Password=1234");
1
  • What exactly do you want to archive? Commented Jun 11, 2016 at 23:50

2 Answers 2

1

Well unless you specify the server name (if you are using linked server) the Tamio.dbo.memberform database is a local database getting used to my knowledge. In case, you want to use some other server DB and you have already defined Linked Server (using sp_addlinkedserver) for that then you can use saying

[server_name].Tamio.dbo.memberform

This is your local connection string and you are using a local DB

SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Tamio;Integrated Security=True");

Whereas, the other one is different server and don't have Tamio DB.

Sign up to request clarification or add additional context in comments.

7 Comments

No i dont want to use linked server. In pc A i have database called Tamio, In pc B i have a database called Imatismos. Also i get this error Could not find server 'USER-PC\SQLEXPRESS. My code : [USER-PC\\SQLEXPRESS].Tamio.dbo.memberform
@user6453809, You don't need to access it that way, since it's a local DB just use it like Tamio.dbo.memberform. no need of including server name. It's get inferred implicitly.
When i use it like this Tamio.dbo.memberform it searchs my database to second pc B . My code is above
Lets say that i want to send from one pc-A to another pc-B through network. How will i do this. I will use something like this? SqlCommand cmd = new SqlCommand("insert into Tamio.memberform.name select Imatismos.customermaster.name ", conn); OR w ecan go in chat if you want
@user6453809, can't go in chat, it's my sleep time (it's highly late night @ my end). per your comment, make sure that the table/DB do exist i other server and yes you can form that query except that you can't directly access it since it's in a different server. That's why in answer I said use linked server. That's the only way to go. once you have created linked server then you can directly say insert into Tamio.memberform.name select * from otherserver.Imatismos.customermaster.name
|
0

Four-part syntax server.db.schema.object to access different server is only applicable if you configured linked server in the server you are connected to. To use ad-hoc connection use OPENDATASOURCE https://msdn.microsoft.com/en-us/library/ms179856.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.