简体   繁体   中英

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.

Also use Parameters instead of 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.

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

"Level" is a keyword in MS Access, may be that is why this issue occurs try quoting it like [Level]

List Of MS Access Keywords

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM