[英]Error connecting to MS Access database from C# Winform
I'm a student programmer and I'm writing this software for a small school, it's my first program, the code below is giving me the error 我是一名学生程序员,我正在为一所小学校编写这个软件,这是我的第一个程序,下面的代码给了我错误
syntax error in insert into statement 插入到语句中的语法错误
I know the connection string is not the problem because I use it for inserting into two other tables with the same insert into format. 我知道连接字符串不是问题,因为我用它插入到另外两个表格中,插入格式相同。
I am using an access database. 我正在使用访问数据库。
The offending code is 违规代码是
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into studentBillRecords (StudentName, Department, Level, AccomodationStatus, SemesterBill, PreviousBalance, TotalBill) values ('"+ txtSRstudentName.Text + "', '" + cmbSRDepartment.Text + "', '" + cmbSRLevel.Text + "', '" + cmbSRAccomodationStatus.Text + "', '" + txtSRSemesterBill.Text + "', '" + txtSRPreviousBalance.Text + "', '" + txtSRTotalBill.Text + "')";
MessageBox.Show(command.CommandText);
command.ExecuteNonQuery();
connection.Close();
This same code with different table names, column names and input works with another table in the same database but won't work with this one. 具有不同表名,列名和输入的相同代码与同一数据库中的另一个表一起使用但不适用于此表。
Level is a reserved keyword in access. Level是访问中的保留关键字。
Also use Parameters instead of concatinating string. 也可以使用Parameters而不是concatinating string。 Try this code out, it makes it safer and easier to read: Note: I changed the name of the column Level to StudentLevel which, I assume, doesn't exist yet in your table. 尝试使用此代码,它使得它更安全,更容易阅读: 注意:我将列Level的名称更改为StudentLevel,我认为,这个名称在您的表中尚不存在。
try
{
using (OleDbConnection connection = new OleDbConnection("my connection string"))
{
//Open connection
connection.Open();
//Create new command
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
//Create command text
cmd.CommandText =
"INSERT INTO studentBillRecords " +
"(StudentName, Department, StudentLevel, AccomodationStatus, SemesterBill, PreviousBalance, TotalBill) VALUES " +
"(@StudentName, @Department, @StudentLevel, @AccomodationStatus, @SemesterBill, @PreviousBalance, @TotalBill)";
// Add names paremeters
cmd.Parameters.AddRange(new OleDbParameter[]
{
new OleDbParameter("@StudentName", txtSRstudentName.Text),
new OleDbParameter("@Department", cmbSRDepartment.Text),
new OleDbParameter("@StudentLevel", cmbSRLevel.Text),
new OleDbParameter("@AccomodationStatus", cmbSRAccomodationStatus.Text),
new OleDbParameter("@SemesterBill", txtSRSemesterBill.Text),
new OleDbParameter("@PreviousBalance", txtSRPreviousBalance.Text),
new OleDbParameter("@TotalBill", txtSRTotalBill.Text)
});
//Execute Query
cmd.ExecuteNonQuery();
//No need to close because we are using "using"
}
}
catch (OleDbException ex)
{
//If an exception occurs let's print it out to console
Console.WriteLine("ERROR: " + ex.ToString());
throw;
}
For information on how to change the column name read this: https://msdn.microsoft.com/en-us/library/bb177883(v=office.12).aspx 有关如何更改列名称的信息,请阅读: https : //msdn.microsoft.com/en-us/library/bb177883(v = office.12).aspx
"Level" is a keyword in MS Access, may be that is why this issue occurs try quoting it like [Level] “Level”是MS Access中的关键字,可能就是这个问题出现的原因尝试引用它如[Level]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.