C# 编写连接字符串以访问远程 SQL Server 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16705303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:42:51  来源:igfitidea点击:

Writing a connection string to access a remote SQL Server database

c#asp.netsql-serverweb-configdatabase-connection

提问by sharon Hwk

My database is hosted on a remote server. I need to write the connection string which I have to include in the web.configfile.

我的数据库托管在远程服务器上。我需要编写必须包含在web.config文件中的连接字符串。

server name -- abcs.efgh.ed-1.eee.sss.com,1433 (it also contains a port as well)
username -- a
password -- a
db name -- mydb

I know how to connect to a local database and I use to refer to connectionstring.comfor reference but, connecting to a remote database is a problem to me. Help please

我知道如何连接到本地数据库,我曾经参考connectionstring.com以供参考,但是,连接到远程数据库对我来说是个问题。请帮忙

UPDATE:

更新

<connectionStrings>                                 
  <add name="DefaultConnection" providerName="System.Data.SqlClient" 
       connectionString="abcs.efgh.ed-1.eee.sss.com,1433;Integrated Security=True;User ID=a;Password=a" />
</connectionStrings>

Exception I get is :

我得到的例外是:

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

登录失败。登录名来自不受信任的域,不能与 Windows 身份验证一起使用。

Actually, it's not Windows authentication that I want to use. It's SQL Server authentication which I want.

实际上,我想使用的不是 Windows 身份验证。这是我想要的 SQL Server 身份验证。

采纳答案by Jay Jay Jay

You are encountering the error because you are using "integrated security=true" to connect. Use this website to construct your connection string. http://www.developerfusion.com/tools/sql-connection-string/

您遇到错误是因为您正在使用“integrated security=true”进行连接。使用此网站构建您的连接字符串。http://www.developerfusion.com/tools/sql-connection-string/

I used the website to generate this connection string using your inputs:

我使用该网站使用您的输入生成此连接字符串:

Data Source=abcs.efgh.ed-1.eee.sss.com,1433;Initial Catalog=mydb;Integrated Security=False;User ID=a;Password=a

回答by Rajeev Kumar

Put Integrated security =false.in connection string

放入Integrated security =false.连接字符串

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used. SqlCredential is a more secure way to specify credentials for a connection that uses SQL Server Authentication (Integrated Security=false).

如果为 false,则在连接中指定用户 ID 和密码。如果为 true,则使用当前的 Windows 帐户凭据进行身份验证。识别的值为true、false、yes、no和sspi(强烈推荐),相当于true。如果指定了用户 ID 和密码并且集成安全设置为 true,则用户 ID 和密码将被忽略并使用集成安全。SqlCredential 是为使用 SQL Server 身份验证 (Integrated Security=false) 的连接指定凭据的更安全方法。

More at Here

更多在这里