簡體   English   中英

使用OleDb的INSERT INTO語句中的語法錯誤

[英]Syntax error in INSERT INTO statement using OleDb

美好的一天。 我正在嘗試創建一個注冊頁面並將信息存儲在數據庫中。 我使用Microsoft Access創建了數據庫。 我明白了:

INSERT INTO語句中的語法錯誤

每次按“注冊”按鈕。 我已經嘗試在網上搜索類似的問題,並發現一些像“保留字”和“它必須是你的間距”。 我做了那些,它仍然給我錯誤。 我錯過了什么嗎?

這是代碼:

public void InsertRecord()
{
    OleDbCommand cmd = new OleDbCommand("INSERT INTO ElemData(StudentID, [Password], [Name], Age, Birthday, Address, FatherName, MotherName, " +
    "GuardianName, Class, Section, Email, PhoneNumber, MobileNumber) " + 
    "VALUES (@studentid, @password, @name, @age, @birth, @address, @father, @mother, @guardian, @classs, @section, @email, @phone, @mobile)", DBConnection.myCon);
    cmd.Parameters.Add("@studentid", OleDbType.VarChar).Value = Studentid;
    cmd.Parameters.Add("@password", OleDbType.VarChar).Value = Password;
    cmd.Parameters.Add("@name", OleDbType.VarChar).Value = Name;
    cmd.Parameters.Add("@age", OleDbType.VarChar).Value = Age;
    cmd.Parameters.Add("@birth", OleDbType.VarChar).Value = Birth;
    cmd.Parameters.Add("@address", OleDbType.VarChar).Value = Address;
    cmd.Parameters.Add("@father", OleDbType.VarChar).Value = Father;
    cmd.Parameters.Add("@mother", OleDbType.VarChar).Value = Mother;
    cmd.Parameters.Add("@guardian", OleDbType.VarChar).Value = Guardian;
    cmd.Parameters.Add("@classs", OleDbType.VarChar).Value = Classs;
    cmd.Parameters.Add("@section", OleDbType.VarChar).Value = Section;
    cmd.Parameters.Add("@email", OleDbType.VarChar).Value = Email;
    cmd.Parameters.Add("@phone", OleDbType.VarChar).Value = Phone;
    cmd.Parameters.Add("@mobile", OleDbType.VarChar).Value = Mobile;
    if (cmd.Connection.State == ConnectionState.Open)
    {
        cmd.Connection.Close();
    }
    DBConnection.myCon.Open();
    cmd.ExecuteNonQuery();
    DBConnection.myCon.Close();
}

ClassSection都是保留字 將它們括在方括號中,就像保留字[Password][Name]

該頁面包含Allen Browne的Database Issue Checker Utility的鏈接。 如果您的Office版本包含Access,則可以下載該實用程序並使用它來檢查Access db文件以查找其他問題對象名稱。

改變你的SQL如下

 OleDbCommand cmd = new OleDbCommand("INSERT INTO ElemData ([StudentID], [Password], [Name], [Age], [Birthday], [Address], [FatherName], [MotherName], " +
    "[GuardianName], [Class], [Section], [Email], [PhoneNumber], [MobileNumber]) " + 
    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", DBConnection.myCon);

當CommandType設置為Text時,OLE DB .NET提供程序不支持將參數傳遞給SQL語句或OleDbCommand調用的存儲過程的命名參數。 在這種情況下,必須使用問號(?)占位符。

暫無
暫無

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

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