1

Is there anything wrong with this code? Please help me out.

protected void Button_Click(object sender, EventArgs e)
{
        string cs = "Data Source=SFSIND0402;Initial Catalog=TestDB;Integrated Security=SSPI;Provider=Microsoft.ACE.OLEDB.12.0";
        OleDbConnection conn = new OleDbConnection(cs);
        conn.Open();
        OleDbCommand insert = conn.CreateCommand();
        insert.CommandText="insert into Employee(ID, Name, Sex, Salary) values('003','Vedpathi','M',25000)";
        insert.Connection = conn;
        insert.ExecuteNonQuery();
        conn.Close();
}

I am getting the following error:

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done

(on line 22:conn.Open();)

3
  • 2
    What kind of database server are you trying to connect to? Commented Jul 16, 2015 at 10:57
  • Is Your provider correct? It seems to me, that You try to connect to Access. The provider should be SQLNCLI10. Isn't it? Commented Jul 16, 2015 at 11:12
  • 2
    Then why are you using an MS ACCESS OLEDB connection string? Commented Jul 16, 2015 at 11:12

2 Answers 2

3

When connecting to an MS SQL database, use the MS SQL providers:

using (var connection = new SqlConnection(connectionString))
{
  connection.Open();

  var cmd = new SqlCommand(commandText, connection);
  cmd.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

Comments

1

In addition to the solution Luaan mentioned, you should store your connection string in the config file of the app and also encrypt it.

Even if you use SSL encryption when communicating with the DB, an ill-indended person can extract the string variables, if he / she runs the application on his / her machine.

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.