krupa.funny
krupa.funny

Reputation: 1

c# code to connect to SQL server which is on different machine

I have created c#.net application with its setup. the setup installs app files and execute database script file. it work proper if SQL server is located on local machine. but when SQL server is located on different machine but in same domain at that time setup fails and database file not get executed. and my application failed to create database.

I need help in this please .....

I used connection string as follows.

connsctionString = "Data Source=filesvr;Integrated Security=SSPI;database=master;"; 

after this my code is

con = new SqlConnection(connsctionString);
con.Open();

when this con.Open() line executes it gives error as

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.

also please confirm is it necessary to provide user id and password in connection string if SQL is on different machine?

Upvotes: 0

Views: 301

Answers (2)

Abdur Rahim
Abdur Rahim

Reputation: 4021

As you are trying to login from a different server, you cannot access the database server with windows authentication. You have to login with a username+password. Here is a sample which you can try.

connsctionString = "Data Source=Server_Name/IP; Initial Catalog=Your_Database;User Id=your_user_name/Id;Password=your_password;pooling='false'; Encrypt=False";

This is tested. Will work for sure. Let me know if it is helpful to you.

Upvotes: 1

Ankit
Ankit

Reputation: 207

i hope it is useful to u

    "Data Source=filesvr;database=master; UId=abc;pwd=xyz";

you can also use ipaddress in data source of the system in which u access sql database

Upvotes: 1

Related Questions