通过 windows 身份验证连接到 sql 时出错
Error while connecting to sql through windows authentication
我正在使用以下代码通过 windows 身份验证连接到 sql。
string connctionstring = "connectionString={0};Database={1};Integrated Security=SSPI;";
string _connctionstring = string.Format(connctionstring, datasource, initialCatalogue);
SqlConnection _connection = new SqlConnection(_connctionstring);
_connection.Open();
但是我收到以下错误。帮助 please.I 我可以通过 sql 服务器登录。
连接字符串format不正确
更改为:
string connctionstring = "Data Source={0};Database={1};Integrated Security=SSPI;";
或者
string connctionstring = "Server={0};Database={1};Integrated Security=SSPI;";
虽然 does cover the basic issue (connectionString
is not a valid key for the string) a better solution is to use a SqlConnectionStringBuilder,如果您的字符串中有奇数字符(例如,如果您的数据库名称包含 space),这也将帮助您进行正确的转义
var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = datasource;
scsb.InitialCatalog = initialCatalogue;
scsb.IntegratedSecurity = true;
//You also really should wrap your connections in using statements too.
using(SqlConnection connection = new SqlConnection(scsb.ConnectionString))
{
connection.Open();
//...
}
我正在使用以下代码通过 windows 身份验证连接到 sql。
string connctionstring = "connectionString={0};Database={1};Integrated Security=SSPI;";
string _connctionstring = string.Format(connctionstring, datasource, initialCatalogue);
SqlConnection _connection = new SqlConnection(_connctionstring);
_connection.Open();
但是我收到以下错误。帮助 please.I 我可以通过 sql 服务器登录。
连接字符串format不正确 更改为:
string connctionstring = "Data Source={0};Database={1};Integrated Security=SSPI;";
或者
string connctionstring = "Server={0};Database={1};Integrated Security=SSPI;";
虽然 connectionString
is not a valid key for the string) a better solution is to use a SqlConnectionStringBuilder,如果您的字符串中有奇数字符(例如,如果您的数据库名称包含 space),这也将帮助您进行正确的转义
var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = datasource;
scsb.InitialCatalog = initialCatalogue;
scsb.IntegratedSecurity = true;
//You also really should wrap your connections in using statements too.
using(SqlConnection connection = new SqlConnection(scsb.ConnectionString))
{
connection.Open();
//...
}