簡體   English   中英

從C#Winform連接到MS Access數據庫時出錯

[英]Error connecting to MS Access database from C# Winform

我是一名學生程序員,我正在為一所小學校編寫這個軟件,這是我的第一個程序,下面的代碼給了我錯誤

插入到語句中的語法錯誤

我知道連接字符串不是問題,因為我用它插入到另外兩個表格中,插入格式相同。

我正在使用訪問數據庫。

違規代碼是

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

具有不同表名,列名和輸入的相同代碼與同一數據庫中的另一個表一起使用但不適用於此表。

Level是訪問中的保留關鍵字。

也可以使用Parameters而不是concatinating string。 嘗試使用此代碼,它使得它更安全,更容易閱讀: 注意:我將列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;
            }

有關如何更改列名稱的信息,請閱讀: https//msdn.microsoft.com/en-us/library/bb177883(v = office.12).aspx

“Level”是MS Access中的關鍵字,可能就是這個問題出現的原因嘗試引用它如[Level]

MS訪問關鍵字列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM