C# 使用服务器的用户名和密码连接到本地 SQL 服务器数据库
C# connecting to local SQL Server database with server's username and password
我正在尝试构建一个自定义连接字符串,将用户登录到特定的 SQL 服务器用户。我尝试了连接字符串的多种变体,但没有任何效果。这是函数。
服务器许可证 - 开发人员
- 服务器类型:本地
- 数据库:NLHospital
- 凭据:从函数传递
在 db.open()
处崩溃;声明。
尝试绕过它并收到此错误消息
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
谁能告诉我我做错了什么?
public string LoginAndGetRole(string username, string password)
{
using (SqlConnection db = new SqlConnection(@"Server=(local)\NLHospital;Integrated Security=false;user id=" + username + ";password=" + password))
{
using (SqlCommand getRole = new SqlCommand("findUserRole", db))
{
db.Open(); //crashes at this line
getRole.CommandType = CommandType.StoredProcedure;
getRole.Parameters.Add(new SqlParameter("@Username", username));
SqlDataReader reader = getRole.ExecuteReader();
}
db.Close();
return "";
}
}
编辑:
1) 我的 SQL 服务器仅配置为 Windows 身份验证。
我认为您的连接字符串有误。您应该在 "Initial Catalog" 或 "Database" 中指定您的数据库名称。
请检查 https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx
处的示例连接字符串
你能试试这个吗?
SqlConnection db = new SqlConnection(@"Data Source=(local); Initial Catalog=NLHospital; Integrated Security=false;user id=" + username + ";password=" + password)
或
SqlConnection db = new SqlConnection(@"Data Source=localhost; Initial Catalog=NLHospital; Integrated Security=false;user id=" + username + ";password=" + password)
您的连接字符串格式似乎有误。请尝试使用以下格式,parameters.Initial Catalog 是您的数据库名称。
= "Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=;
首先,请检查NLHospital 是您的数据库实例名称吗?或您的数据库名称?
给你举个例子。
Data Source=ServerName\InstanceName;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True
我正在尝试构建一个自定义连接字符串,将用户登录到特定的 SQL 服务器用户。我尝试了连接字符串的多种变体,但没有任何效果。这是函数。
服务器许可证 - 开发人员
- 服务器类型:本地
- 数据库:NLHospital
- 凭据:从函数传递
在 db.open()
处崩溃;声明。
尝试绕过它并收到此错误消息
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
谁能告诉我我做错了什么?
public string LoginAndGetRole(string username, string password)
{
using (SqlConnection db = new SqlConnection(@"Server=(local)\NLHospital;Integrated Security=false;user id=" + username + ";password=" + password))
{
using (SqlCommand getRole = new SqlCommand("findUserRole", db))
{
db.Open(); //crashes at this line
getRole.CommandType = CommandType.StoredProcedure;
getRole.Parameters.Add(new SqlParameter("@Username", username));
SqlDataReader reader = getRole.ExecuteReader();
}
db.Close();
return "";
}
}
编辑: 1) 我的 SQL 服务器仅配置为 Windows 身份验证。
我认为您的连接字符串有误。您应该在 "Initial Catalog" 或 "Database" 中指定您的数据库名称。
请检查 https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx
处的示例连接字符串你能试试这个吗?
SqlConnection db = new SqlConnection(@"Data Source=(local); Initial Catalog=NLHospital; Integrated Security=false;user id=" + username + ";password=" + password)
或
SqlConnection db = new SqlConnection(@"Data Source=localhost; Initial Catalog=NLHospital; Integrated Security=false;user id=" + username + ";password=" + password)
您的连接字符串格式似乎有误。请尝试使用以下格式,parameters.Initial Catalog 是您的数据库名称。
= "Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=;
首先,请检查NLHospital 是您的数据库实例名称吗?或您的数据库名称?
给你举个例子。
Data Source=ServerName\InstanceName;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True