[英]Can't connect Visual Studio to SQL Server database
I use Visual Studio 2017, I connected to my database from Server Explorer: 我使用Visual Studio 2017,并从服务器资源管理器连接到数据库:
.
。
Then I added SQL connection: 然后我添加了SQL连接:
var constring = @"Data Source=JSINJARA\SQLEXPRESS,Database=box,Integrated Security=SSPI";
SqlConnection con = new SqlConnection(constring);
But when I try to open connection I get the following exception: 但是,当我尝试打开连接时,出现以下异常:
What can be the problem? 可能是什么问题?
Use ;
使用
;
(semicolon) instead of ,
(comma) in the connection string. 代替(分号)
,
在连接字符串中(逗号)。
Eg 例如
var constring = @"Data Source=JSINJARA\SQLEXPRESS;Database=box;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(constring);
See the Connection Strings website for many examples. 有关许多示例,请参见连接字符串网站 。
Better add it to [app/web].config file and get it using ConfigurationManager
. 最好将其添加到[app / web] .config文件中,并使用
ConfigurationManager
获取。
It's should look something like this: 它看起来应该像这样:
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=<<Server>>;Initial Catalog=<<DBName>>;User=<<User>>;Password=<<Password>>;"/>
</connectionStrings>
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
using(SqlConnection con = new SqlConnection(connectionString))
{
//Your code goes here
}
尝试在连接中设置Integrated Security=True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.