c# WinForms不接受连接字符串用户详细信息

本文关键字:用户 详细信息 字符串 连接 WinForms 不接受 | 更新日期: 2023-09-27 18:10:19

我有一个连接字符串在App.config像这样:

<add name="connectionString"
      connectionString="Data Source=SERVER;Initial Catalog=DB;Integrated Security=True;User ID=domain'username;Password=12345;Connection Timeout=300"
      providerName="System.Data.SqlClient" />

然后在后面的代码中像这样调用字符串:

string conSTR = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                SqlDataReader reader;
                using (SqlConnection sqlConn = new SqlConnection(conSTR))
                using (SqlCommand cmd = new SqlCommand(SQLQuery, sqlConn))
                {
                    sqlConn.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                     .... stuff happens here
                    }
                }

我的本地帐户没有访问服务器的权限,但是传递给连接字符串的服务帐户具有访问权限。

我得到的错误是:
Cannot open database "DB" requested by the login. The login failed.
Login failed for user 'domain'MyUserName'.

由于某些原因,它完全忽略了连接字符串中的用户名/密码,并尝试使用我的帐户进行连接。

我怎样才能解决这个问题?

c# WinForms不接受连接字符串用户详细信息

如果您的连接字符串中集成了security = true,请删除它。

这是因为您指定的是Integrated Security=True;。这将推翻任何其他身份验证设置,删除它,它将使用提供的用户名和密码。