C# 用户登录失败';sa';。
我尝试通过调用以下类连接到sql server:C# 用户登录失败';sa';。,c#,sql-server,C#,Sql Server,我尝试通过调用以下类连接到sql server: class DataCon { public static SqlConnection Connection { get; private set; } public static void connectDB(string ip, string dbName, string user, string pw) { string connectionSt = "Server="+ip+";Database=
class DataCon
{
public static SqlConnection Connection { get; private set; }
public static void connectDB(string ip, string dbName, string user, string pw)
{
string connectionSt = "Server="+ip+";Database="+dbName+";User Id="+user+";Password="+pw+";";
ConnectToSql(connectionSt);
}
private static void ConnectToSql(string connectionSt) {
Connection = new SqlConnection(connectionSt);
Connection.Open();
}
public static void connectDB(string ip, string dbName)
{
string connectionSt = "Server="+ip+";Database="+dbName+";Trusted_Connection=True;";
ConnectToSql(connectionSt);
}
}
它在使用Windows身份验证时工作,但为用户“sa”提供登录的服务器身份验证失败
在SQLServerManagementStudio上,我可以使用Windows和“sa”服务器身份验证成功登录
这是我使用的表格:
private void btnConfirm_Click(object sender, EventArgs e)
{
try
{
string dbsName = "dbsStudent";
string ip = "localhost";
string user = txtUser.Text;
string pw = txtUser.Text;
if (cbAuthentication.SelectedIndex == 0)
DataCon.connectDB(ip,dbsName);
else
DataCon.connectDB(ip,dbsName,user,pw);
this.Hide();
new ViewSt().Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
如果密码包含特殊字符,如“;”,您的连接字符串将不正确
考虑使用SqlConnectionStringBuilder类来构建连接字符串,如中所述。也许您应该看看 在中指定了false、用户ID和密码时的集成安全性 连接。如果为true,则当前Windows帐户凭据为 用于身份验证。识别值为真、假、是、否, 和sspi(强烈建议),这相当于true。如果用户 指定ID和密码,并将集成安全设置为true, 用户ID和密码将被忽略,集成安全性将被忽略 被使用 因此,如果您想使用用户“sa”登录到数据库,您可能应该在连接字符串中将integratedsecurity设置为false,并尝试解决这个问题:)
如果您有任何问题,请在这里给我留言。我一点也不懂大拇指向下的意思。我说我可以和那个“sa”用户一起登录management studio,但不能用C#……。你的问题在这个链接中:我知道你会指向我已经读过的两个问题。。。。。这不是同一个问题,伙计