Every one i write code that insert some data into Microsoft Access database but i have an error "Syntax error in insert into statement" i don't know why !!! Any one help me ? thanks in advance ; code:
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\me\Library Store\Library Store\Store.accdb");
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Libarary ( ISBN, Name, Gategory, Author, Cost, Date) VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";
cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
cmd.Parameters.AddWithValue("@Name", NameTB.Text);
cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
cmd.Parameters.AddWithValue("@Cost", int.Parse(CostTB.Text));
cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added!");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
When one or more of your fields use a reserved keyword you need to enclose ALWAYS that field in square brackets. (A very annoying problem). In your query, you use two reserved keywords: DATE and NAME
cmd.CommandText = "INSERT INTO Libarary ( ISBN, [Name], Gategory, Author, Cost, [Date]) " +
"VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";
If it is not too late, I suggest you to rename these fields to avoid this kind of problem in future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.