简体   繁体   English

C#-“用户'用户名'登录失败(SQL Server 2014)

[英]C# - "Login failed for user 'Username'(SQL Server 2014)

I'm trying to connect to SQL Server 2014 using my C# code 我正在尝试使用C#代码连接到SQL Server 2014

<connectionStrings>
    <add name="ConnectionString" 
         connectionString="Data Source=IP\\SQLNameSERVER,1433;Network Library=DBMSSOCN; Initial Catalog=MyDB; User ID=Username; Password=password;"/>
</connectionStrings>

string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
     query = "SELECT * FROM [MyTable]";

     using (SqlCommand commandUserPortal = new SqlCommand(query, cn))
     {
         cn.Open();
     }
  }

but I get an error message: 但我收到一条错误消息:

Login failed for user 'Username'. 用户“用户名”的登录失败。

When I use the same credentials to connect to DB through SQL Server Management Studio, it works fine without any issues. 当我使用相同的凭据通过SQL Server Management Studio连接到DB时,它工作正常,没有任何问题。

The user has db_datareader permission. 用户具有db_datareader权限。

I also tried 我也试过

<connectionStrings>
    <add name="ConnectionString" 
         connectionString="Data Source=IP\\SQLNameSERVER,1433;Network Library=DBMSSOCN; Initial Catalog=MyDB; User ID=Username; Password=password;
         providerName="System.Data.SqlClient"/>
</connectionStrings>

Use an accurate connection string via using Add connection from Visual studio. 通过使用Visual Studio中的“ Add connection来使用准确的连接字符串。

How:- 怎么样:-

Follow the next screen shots:- 跟随下一个屏幕截图:

1) View >> Server Explorer 1)查看>>服务器资源管理器

在此处输入图片说明

2) Add Connection 2)添加连接

在此处输入图片说明

3) Choose data source >> SQL Server 3)选择数据源>> SQL Server

在此处输入图片说明

4) Type Server Name , SQL Server authentication , type username and password , and choose database . 4)键入Server NameSQL Server authentication ,键入usernamepassword ,然后选择database

在此处输入图片说明

5) Click Test Connection button. 5)单击Test Connection按钮。

在此处输入图片说明

6) The Connection that you created will be added here 6)您创建的连接将添加到此处

在此处输入图片说明

7) Right click and choose properties 7)右键单击并选择properties

在此处输入图片说明

8) Finally copy and paste the connection string and use it , and replace the stars (********) with your password 8) 最后复制并粘贴连接字符串并使用它, 然后用密码替换星号(********)

Try to change setting in sql server for you database. 尝试为您的数据库更改sql server中的设置。 Allow both sql and windows authentication. 允许sql和Windows身份验证。 For steps with images, check here . 有关图像的步骤,请在此处检查。 I hope that you are not using windows authentication and also the DB server is reachable by your network. 我希望您不使用Windows身份验证,并且您的网络也可以访问数据库服务器。 Everything else is looking ok to me. 其他一切对我来说都很好。

try to change your Connection String: 尝试更改您的连接字符串:

<add name="ConnectionString" connectionString="Data source=IP\SQLNameSERVER;Initial Catalog=DBname;User=Username;Password=Password; connection timeout=6000;" />

Also try to change your Code: 同时尝试更改您的代码:

from: 从:

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
     query = "SELECT * FROM [MyTable]";

     using (SqlCommand commandUserPortal = new SqlCommand(query, cn))
     {
         cn.Open();
     }
  }

to: 至:

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
     query = "SELECT * FROM [MyTable]";
     cn.Open();
     using (SqlCommand commandUserPortal = new SqlCommand(query, cn))
     {

     }
  }

Use the following as Connection String 使用以下内容作为连接字符串

<add name="DefaultConnection" connectionString="data source=192.168.0.1; initial catalog=DBNAME;persist security info=True;user id=UserId;password=Password;MultipleActiveResultSets=True ; Connect Timeout=10000" providerName="System.Data.SqlClient" />

Replace your Database name in the catalog,user id in user id and passeord in password 在目录中替换您的数据库名称,在用户ID中替换用户ID,并在密码中替换passeord

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

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