INSERT INTO表(列)VALUE(电子邮件)

时间:2017-05-29 14:11:39

标签: c# ms-access insert-into oledbcommand

我想在Table中插入只有一列的电子邮件。我试过2路。我第一次尝试使用commandText,第二次尝试使用parapeters。但这两种解决方案都给了我同样的错误。

  

System.Data.OleDb.OleDbException:'INSERT INTO语句中的语法错误。'

我在INSERT STATEMENT中没有看到任何错误。也许问题出现在TABLE?

 using (OleDbCommand cmd = new OleDbCommand())
 {
    cmd.Connection = conn;
    cmd.CommandText = "SELECT COUNT (email) FROM [User] WHERE [email] LIKE '" + userEmail + "';";
    conn.Open();
    int count = Convert.ToInt32(cmd.ExecuteScalar()); // This passed
    if (count == 0)
    {
        string query = @"INSERT INTO User (email) VALUES (@email)";
        string cmdText= @"INSERT INTO User (email) VALUES ('"+userEmail+"')";
        OleDbCommand command = new OleDbCommand(cmdText, conn);
   //     command.Parameters.AddWithValue("@email", "userEmail");
       // command.CommandText = query;
        command.ExecuteNonQuery(); // I GOT ERROR HERE
    }
    conn.Close();
}

2 个答案:

答案 0 :(得分:3)

User是一个关键字。您应该INSERT INTO [USER]而不是

答案 1 :(得分:0)

string cmdText= @"INSERT INTO User (email)) VALUES ('"+userEmail+"')";

你有一个')'太多了(电子邮件)