1

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.

this error appeared by using this code

error : system.Data.sqlclint.sqlexpection(0x80131904) cannot open database "J_InsData" request by the login. the login faild login failed for user J-PC\J

code: J_connetionString1 = @"Data Source=.\SQLEXPRESS;Initial Catalog=J_InsData;Trusted_Connection=True;Integrated Security=SSPI";

1
  • 3
    Use following connection string @"Data Source=.\SQLEXPRESS;Initial Catalog=J_InsData;Integrated Security=SSPI"; Commented Jan 20, 2014 at 15:57

2 Answers 2

2

You need to tell SQL Server which user credentials to use for a connection.

Hence you should provide a user id and password of SQL Server user in your connection string.

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.

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Server=myServerAddress;Database=myDataBase;Integrated Security=SSPI;
Sign up to request clarification or add additional context in comments.

Comments

1

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.

This LINK might be useful for you as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.