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

时间:2015-12-26 07:18:16

标签: c# mysql sql-server winforms ms-access-2010

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

  插入语句

中的

语法错误

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

我正在使用访问数据库。

违规代码是

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

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

2 个答案:

答案 0 :(得分:2)

级别是访问中的保留关键字。

也可以使用Parameters而不是concatinating string。尝试使用此代码,使其更安全,更易于阅读: 注意:我将列级别的名称更改为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;
            }

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

答案 1 :(得分:1)

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

<强> List Of MS Access Keywords