我正在尝试检查(在 .net c# 中)是否能够连接到 SQL 服务器。但是,每当我在连接字符串中指定实例时,我就无法再连接。:
这有效:
builder.ConnectionString = "Server=DLS-534;user id=sa;password=Mypassword;initial catalog=master";
这不起作用:
builder.ConnectionString = "Server=DLS-534\\SQL_2008_R2_DEV;user id=sa;password=Mypassword;initial catalog=master";
为什么这不起作用???我确实需要能够连接到特定实例,因为用户可能有多个数据库。
我的完整代码:
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder();
builder.ConnectionString = "Server=DLS-534\\SQL_2008_R2_DEV;user id=sa;password=Mypassword;initial catalog=master";
using (var connection = new SqlConnection(builder.ConnectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}