FROM子句中的语法

时间:2014-03-14 14:10:58

标签: c# sql ms-access-2007

private void DeleteQuestion_Click(object sender, EventArgs e)
{
    try
    {
        using (OleDbConnection conn = new OleDbConnection(access7ConnectionString))
        {
            conn.Open();

            using (OleDbCommand cmd = new OleDbCommand("DELETE FROM Questions" + "WHERE ID=@ID", conn))
            {
                cmd.Parameters.AddWithValue("@ID", currentRecord);
                cmd.ExecuteNonQuery();
            }
        }
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

我收到此错误:

Syntax in FROM Clause

我哪里错了?还在学习C#

access7ConnectionString是数据库的字符串路径,我已将其删除以缩短帖子,但建立了与数据库的连接并且一切正常。

2 个答案:

答案 0 :(得分:2)

Table Name和WHERE之间没有空格,请放置如下空格:

"DELETE FROM Questions" + " WHERE
                        ///^^^

不确定为什么必须使用字符串连接。这一切都可以与单个字符串相提并论。像:

using (OleDbCommand cmd = new OleDbCommand("DELETE FROM Questions WHERE ID=@ID", conn))

答案 1 :(得分:2)

您缺少空格,请更改

"DELETE FROM Questions" + "WHERE ID=@ID"

"DELETE FROM Questions WHERE ID=@ID"