简体   繁体   English

使用Visual Studio远程连接到SQL Server数据库

[英]Remote connection to SQL Server database using Visual Studio

On Visual Studio remote server is added but can't access databases in that server. 在Visual Studio上,添加了远程服务器,但无法访问该服务器中的数据库。

On clicking "Test Connection" it shows this error: 在单击“测试连接”时,显示此错误:

Login failed .The login is from an untrusted domain and cannot be used with Windows Authentication 登录失败。登录来自不受信任的域,不能与Windows身份验证一起使用

How to establish a connection to a remote SQL Server database using Visual Studio? 如何使用Visual Studio建立与远程SQL Server数据库的连接?

To create a data connection to a SQL Server database: 创建与SQL Server数据库的数据连接:

  • In Server Explorer/Database Explorer click Connect to Database. 在服务器资源管理器/数据库资源管理器中,单击“连接到数据库”。
  • In the Choose Data Source dialog box, select Microsoft SQL Server, and then click OK. 在“选择数据源”对话框中,选择“ Microsoft SQL Server”,然后单击“确定”。
  • 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). 根据您的数据库或应用程序的要求,选择Windows身份验证或使用特定的用户名和密码登录到SQL Server(SQL Server身份验证)。
  • 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 ? 此外,是否正在运行XAMP或WAMP之类的服务器管理器?

It seems that the SQL Server and the development computer belong to different AD domains that do not have a trust relationship. 似乎SQL Server和开发计算机属于不具有信任关系的不同AD域。 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. 如果您不能在域之间添加信任关系(您可能需要询问IT部门),则可以使用SQL身份验证在SQL Server上创建可在连接字符串中使用的帐户。 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. 从上面的示例中可以看到,使用Windows身份验证的一大优势是您不必将密码存储在连接字符串中。 If using SQL authentication, it is strongly advised to encrypt the connection strings. 如果使用SQL身份验证,强烈建议对连接字符串进行加密。

See this link on how to configure Mixed authentication mode on a SQL Server so that you can use SQL authentication. 有关如何在SQL Server上配置混合身份验证模式的信息,请参见此链接 ,以便可以使用SQL身份验证。

There is a special account usually called sa . 有一个特殊帐户,通常称为sa This is the sysadmin account that has all permissions. 这是具有所有权限的sysadmin帐户。 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. 虽然它是测试身份验证问题的一个好帐户,但建议不要在应用程序中使用它,因为潜在的攻击者可能会使用此帐户来严重破坏SQL Server安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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