问题从文本框插入数据到sqlite表

时间:2015-01-15 04:56:59

标签: c# sqlite

我正在尝试将数据输入到sqlite数据库中。我正在使用以下代码,但我正在做的事情是错误的,它无法正常工作。

  string dbConnection = "Data Source=" + dbString + ";Version=3;";
        using (SQLiteConnection myConnection = new SQLiteConnection(dbConnection))
        {
            SQLiteCommand sqlCommand = new SQLiteCommand("INSERT INTO Products (First_Name,Last_Name,Company_Name,Address1,Address2,City,State,Zip,Phone_Number1,Phone_Number2,Email_Address) Values (@First_Name, @Last_Name,@Company_Name, @Address1,@Address2, @City,@State, @Zip, @Phone_Number1, @Phone_Number2, @Email_Address,)", myConnection);
            sqlCommand.Parameters.AddWithValue("@First_Name", FNAME_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Last_Name", LNAME_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Company_Name", CNAME_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Address1", ADDRESS1_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Address2", ADDRESS2_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@City", CITY_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@State", STATE_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Zip", ZIP_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Phone_Number1", PHONE1_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Phone_Number2", PHONE2_TXT.Text);
            sqlCommand.Parameters.AddWithValue("@Email_Address", EMAIL_TXT.Text);

            myConnection.Open();
            return sqlCommand.ExecuteNonQuery();

当我执行代码时,我得到:

"类型为System.Data.SQLite.SQLiteException'的第一次机会异常。发生在System.Data.SQLite.dll

其他信息:SQL逻辑错误或缺少数据库

near")":语法错误"

这是创建数据库的代码:

      sqlite_conn = new SQLiteConnection(dbLoc);
                sqlite_conn.Open();
                sqlite_cmd = sqlite_conn.CreateCommand();
                sqlite_cmd.CommandText = "CREATE TABLE Customer ("
                        + "Customer_ID_PK integer primary key,"
                        + "First_Name VARCHAR(32),"
                        + "Last_Name VARCHAR(32) NOT NULL,"
                        + "Company_Name VARCHAR(50),"
                        + "Address1 VARCHAR(45) NOT NULL,"
                        + "Address2 VARCHAR(45),"
                        + "City VARCHAR(25) NOT NULL,"
                        + "State VARCHAR(2) NOT NULL,"
                        + "Zip CHAR(5) NOT NULL,"
                        + "Phone_Number1 CHAR(10) NOT NULL,"
                        + "Phone_Number2 CHAR(10),"
                        + "Email_Address VARCHAR(25))";
                sqlite_cmd.ExecuteNonQuery();
                sqlite_conn.Close();    

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

删除最后一个逗号

... @Email_Address,)"

 @Email_Address)"

答案 1 :(得分:0)

在查询中删除额外的逗号。

改变这个:

INSERT INTO Products (First_Name,Last_Name,Company_Name,Address1,Address2,City,State,Zip,Phone_Number1,Phone_Number2,Email_Address) Values (@First_Name, @Last_Name,@Company_Name, @Address1,@Address2, @City,@State, @Zip, @Phone_Number1, @Phone_Number2, @Email_Address,)

为:

INSERT INTO Products (First_Name,Last_Name,Company_Name,Address1,Address2,City,State,Zip,Phone_Number1,Phone_Number2,Email_Address) Values (@First_Name, @Last_Name,@Company_Name, @Address1,@Address2, @City,@State, @Zip, @Phone_Number1, @Phone_Number2, @Email_Address)