Reputation: 543
What am I not understanding in the following code:
using (OleDbConnection connectionFail = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Fail_DB.mdb"))
{
connectionFail.Open();
var cmd = new SqlCommand("INSERT INTO SUMMARY (A, B, C) VALUES (@A, @B, @C)");
cmd.Parameters.Add("@A", SqlDbType.NVarChar, 50).Value = "A";
cmd.Parameters.Add("@B", SqlDbType.NVarChar, 50).Value = "B";
cmd.Parameters.Add("@C", SqlDbType.NVarChar, 50).Value = "C";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
connectionFail.Close();
cmd.Dispose();
}
connectionFail.Close();
}
}
I just want to add data to a SUMMARY table. I get ExecuteNonQuery:Connection property has not been initialized. What am I not understanding?
Upvotes: 0
Views: 68
Reputation: 131227
The connection was created but never set on the command object. BTW SqlCommand is a SQL Server Client command.
Instead of :
var cmd = new SqlCommand("INSERT INTO SUMMARY (A, B, C) VALUES (@A, @B, @C)");
Use
var cmd = new OleDbCommand("INSERT INTO SUMMARY (A, B, C) VALUES (?, ?, ?)",connection);
Access doesn't support named parameters in SQL queries. The parameter values will be substitued by position.
Upvotes: 1