"INSERT INTO语句中的语法错误"将记录添加到Access数据库时 [英] "Syntax error in INSERT INTO statement" when adding record to Access database

查看:140
本文介绍了"INSERT INTO语句中的语法错误"将记录添加到Access数据库时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了数小时以寻求解决此问题的方法,但是我所读的书没有任何帮助.尝试将此记录添加到Access数据库时出现此错误.我要保存到的文件名为Cats.accdb,带有名为Cats的表.

I've searched for hours for a solution to this problem but nothing I've read has helped. I'm getting this error when trying to add this record to an Access database. The file I'm trying to save into is named Cats.accdb, with a table named Cats.

表列名称:
CatId(类型:文本)
CatName(文本)
头发(文本)
大小(文本)
CareType(文本)
注释(文本)
AdoptDate(日期/时间一般日期),Weight(加倍),Age(整数)(我已经在C#代码中注释了对这些列的任何引用,以尝试仅使用简单的旧文本框进行调试.起初,我认为这是因为与使用DateTimePicker有关,但是在注释掉后仍然会引发错误.)

Table column names:
CatId (type: text)
CatName (text)
Hair (text)
Size (text)
CareType (text)
Notes (text)
AdoptDate (date/time general date), Weight (double), Age (integer) (I've commented any reference to these columns out in the C# code to attempt to debug with just plain old text boxes. At first I thought it was because of something to do with using a DateTimePicker, but it still throws the error after commenting out.)

C#代码:

Cat temp = new Cat(txtCatName.Text, txtHair.Text, txtSize.Text, txtCareType.Text, txtNotes.Text);

public string AddCat()
    {
        string strFeedback = "";

        string strSQL = "INSERT INTO Cats (CatName, Hair, Size, CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

        OleDbConnection conn = new OleDbConnection();

        string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Data\Cats.accdb; Persist Security Info=False;";
        conn.ConnectionString = strConn; 

        OleDbCommand comm = new OleDbCommand();
        comm.CommandText = strSQL;  
        comm.Connection = conn; 
        comm.Parameters.AddWithValue("@CatName", CatName); 
        comm.Parameters.AddWithValue("@Hair", Hair);
        comm.Parameters.AddWithValue("@Size", Size);
        comm.Parameters.AddWithValue("@CareType", CareType);
        comm.Parameters.AddWithValue("@Notes", Notes);
        //comm.Parameters.AddWithValue("@AdoptDate", AdoptDate);
        //comm.Parameters.AddWithValue("@Weight", Weight);
        //comm.Parameters.AddWithValue("@Age", Age);

        {
            conn.Open();
            strFeedback = comm.ExecuteNonQuery().ToString() + " record has been added successfully!";
            conn.Close();
        }
        catch (Exception err)
        {
            strFeedback = "ERROR: " + err.Message;
        }
        return strFeedback;

lblFeedback.Text = temp.AddCat();

感谢您提供的任何帮助!

Thanks for any help you can give!

推荐答案

Size保留关键字.在名称周围加上方括号以指定它是标识符:

Size is a reserved keyword. Add brackets around the name to specify that it's an identifier:

string strSQL = "INSERT INTO Cats (CatName, Hair, [Size], CareType, Notes) VALUES (@CatName, @Hair, @Size, @CareType, @Notes)"; 

或者,将字段名称更改为不是关键字的名称.

Alternatively, change the field name to something that is not a keyword.

这篇关于"INSERT INTO语句中的语法错误"将记录添加到Access数据库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆