简体   繁体   English

用户C#登录失败

[英]Login failed for user C#

protected DataTable GetData(string Query)
{
          DataTable data = new DataTable();
          try
          {
                strConnectionString = "data source=(local);Integrated Security=True;initial catalog=ps_erdb;";
                string Query = "Select ErdbBuildNumbereMinor from DBVersion";
                using (sqlConnection = new SqlConnection(strConnectionString))
                {
                    sqlConnection.Open();
                    SqlCommand cmd = sqlConnection.CreateCommand();
                    cmd.CommandTimeout = intConnectionTimeout;
                    cmd.CommandText = Query;
                    data.Load(cmd.ExecuteReader());
                    sqlConnection.Close();
                }
                return data;
            }
            catch (Exception ex)
            {
        }
        return data;
}

the above code returns a error 上面的代码返回错误

Cannot open database "ps_erdb" requested by the login. 无法打开登录请求的数据库“ ps_erdb”。 The login failed. 登录失败。 Login failed for user 'MIG3-ESVTB\\HPSInstall'. 用户“ MIG3-ESVTB \\ HPSInstall”的登录失败。

But when i manually open SQL Server Management Studio with above login ,i was able to connect to database and execute the query. 但是,当我使用上述登录名手动打开SQL Server Management Studio时,我能够连接到数据库并执行查询。

A few possibilities to look into: 有几种可能性可以研究:

  1. What user were you logged in with on your local client? 您在本地客户端上使用什么用户登录? Your connection string indicates you're using integrated security so the application will present your current logged in credentials to SQL Server. 您的连接字符串表示您正在使用集成安全性,因此该应用程序将向SQL Server提供您当前登录的凭据。 To help diagnose, make sure you enable login auditing for both failed & successful logins. 为了帮助诊断,请确保同时为失败和成功的登录启用登录审核。 When you connect from SSMS and run SELECT suser_name(), what does it return? 从SSMS连接并运行SELECT suser_name()时,它返回什么?

  2. Is the database set for multi_user mode (default) or single_user mode? 数据库设置为多用户模式(默认)还是单用户模式? Someone may have changed it to single_user. 可能有人将其更改为single_user。 This allows the first and only one connection. 这允许第一个也是唯一一个连接。 All subsequent attempts will fail. 所有后续尝试将失败。 If it's in single user mode, just run ALTER DATABASE ps_erdb SET MULTI_USER from SSMS 如果在单用户模式下,只需从SSMS运行ALTER DATABASE ps_erdb SET MULTI_USER

  3. When you login via SSMS, were you able to query data from the ps_erdb database? 通过SSMS登录时,是否可以从ps_erdb数据库查询数据? If not, make sure you have a database user mapped to the 'MIG3-ESVTB\\HPSInstall' login and the appropriate permissions granted to the user (not the login). 如果不是,请确保您有一个数据库用户映射到“ MIG3-ESVTB \\ HPSInstall”登录名,并已授予该用户相应的权限(不是登录名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM