从 C# Winform 连接到 MS Access 数据库时出错

Error connecting to MS Access database from C# Winform

我是一名学生程序员,我正在为一所小学校编写这个软件,这是我的第一个程序,下面的代码给我错误

syntax error in insert into statement

我知道连接字符串不是问题,因为我用它插入到另外两个具有相同插入格式的 table。

我正在使用访问数据库。

违规代码是

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();

具有不同 table 名称、列名和输入的相同代码可与同一数据库中的另一个 table 一起使用,但不适用于此代码。

Level是access中的保留关键字。

也使用参数而不是连接字符串。试试这段代码,它使它更安全、更容易阅读: 注意: 我将 Level 列的名称更改为 StudentLevel,我想这在您的 table.

中尚不存在
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;
            }

有关如何更改列名称的信息,请阅读: https://msdn.microsoft.com/en-us/library/bb177883(v=office.12).aspx

"Level" 是 MS Access 中的关键字,可能这就是出现此问题的原因,请尝试将其引用为 [Level]

List Of MS Access Keywords