简体   繁体   中英

Remote connection to SQL Server database using Visual Studio

On Visual Studio remote server is added but can't access databases in that server.

On clicking "Test Connection" it shows this error:

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

How to establish a connection to a remote SQL Server database using Visual Studio?

To create a data connection to a SQL Server database:

  • In Server Explorer/Database Explorer click Connect to Database.
  • In the Choose Data Source dialog box, select Microsoft SQL Server, and then click OK.
  • Select a server name from the drop-down list, or type the name of the server where the database you want to access is located.
  • Based on the requirements of your database or application, select either Windows Authentication or use a specific user name and password to log on to the SQL Server (SQL Server Authentication).
  • Select the database you want to connect to from the drop-down list.
  • Click OK

The Cooler Syntax Way:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "connection_string";
conn.Open();

// use the connection here

conn.Close();
conn.Dipose();

Executing commands:

SqlCommand command = new SqlCommand("SELECT * FROM TableName", conn);

Also, are running a server manager like XAMP or WAMP ?

It seems that the SQL Server and the development computer belong to different AD domains that do not have a trust relationship. A trust relationship between domains is used so that you can log into computers with accounts from a trusted domain.

If you cannot add a trust relationship between the domains (you might need to ask your IT department for this), you can use SQL authentication to create accounts on the SQL Server that you can use in the connection string. A typical connection string looks like this:

Data Source=MyServer\MyInstance;Initial Catalog=MyDatabase;User Id=MySQLAccount;Password=MyPassword;

As you see from the sample above, a big advantage of using Windows authentication is that you do not have to store the password in the connection string. If using SQL authentication, it is strongly advised to encrypt the connection strings.

See this link on how to configure Mixed authentication mode on a SQL Server so that you can use SQL authentication.

There is a special account usually called sa . This is the sysadmin account that has all permissions. While a good account for testing authentication issues, it is advised to not use it in applications as a potential attacker could use this account to damage a SQL server installation heavily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM