简体   繁体   English

SQLConnection错误“用户登录失败” C#

[英]error “Login failed for user” C# with SQLConnection

I created a database (without user or password) as a service-based database. 我创建了一个数据库(没有用户名或密码)作为基于服务的数据库。

Now I'm trying to insert to a database, but there is a error "Login failed for user" 现在,我尝试插入数据库,但是出现错误“用户登录失败”

I have this code: 我有以下代码:

string J_connetionString1 = null;
SqlConnection J_connection1;
SqlDataAdapter J_adapter1 = new SqlDataAdapter();
string J_sql1 = null;
J_connetionString1 = @"Data Source=.\SQLEXPRESS;Initial Catalog=J_InsData";

J_connection1 = new SqlConnection(J_connetionString1);
J_sql1 = "update instinfo set instinfoNAME = textBox2.text where instinfoID ='1'";
try
{
    J_connection1.Open();
    J_adapter1.UpdateCommand = J_connection1.CreateCommand();
    J_adapter1.UpdateCommand.CommandText = J_sql1;
    J_adapter1.UpdateCommand.ExecuteNonQuery();
    MessageBox.Show("Row updated !! ");
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

i can't comment so i edited the Q. 我无法发表评论,所以我编辑了Q。

this error appeared by using this code 通过使用此代码出现此错误

error : system.Data.sqlclint.sqlexpection(0x80131904) cannot open database "J_InsData" request by the login. 错误:system.Data.sqlclint.sqlexpection(0x80131904)无法通过登录名打开数据库“ J_InsData”请求。 the login faild login failed for user J-PC\\J 登录失败用户J-PC \\ J的登录失败

code: J_connetionString1 = @"Data Source=.\\SQLEXPRESS;Initial Catalog=J_InsData;Trusted_Connection=True;Integrated Security=SSPI"; 代码:J_connetionString1 = @“数据源=。\\ SQLEXPRESS;初始目录= J_InsData; Trusted_Connection = True;集成安全性= SSPI”;

You need to tell SQL Server which user credentials to use for a connection. 您需要告诉SQL Server哪个用户凭据用于连接。

Hence you should provide a user id and password of SQL Server user in your connection string. 因此,您应该在连接字符串中提供SQL Server用户的用户ID和密码。

Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;

On the other hand if the SQL erver is configured to accept windows domain users, you can use Integrated Security=SSPI or Trusted_Connection=True in the connection string. 另一方面,如果将SQL erver配置为接受Windows域用户,则可以在连接字符串中使用Integrated Security = SSPI或Trusted_Connection = True。

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Server=myServerAddress;Database=myDataBase;Integrated Security=SSPI;

Have you tried adding the following to your connection string? 您是否尝试过将以下内容添加到连接字符串中?

Integrated Security=True

or 要么

Integrated Security=SSPI

which is pretty mcuh the same as True. 这与True相当。

This LINK might be useful for you as well. 链接也可能对您有用。

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

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