C#解析到查询时出错

C#解析到查询时出错,c#,sql,sql-server-ce,C#,Sql,Sql Server Ce,System.Data.SqlServerCeException(0x80004005):分析查询时出错。[令牌行编号=1,令牌行偏移量=120,令牌出错=@price] 我在运行我的程序时不断遇到这个错误,我无法找出我做错了什么。我尝试过改变多个方面,但它总是给出相同的错误,只是“tokenline offset=”后面的数字有时会改变 static public void Insert(string _id, string _city, string _state, string _coun

System.Data.SqlServerCeException(0x80004005):分析查询时出错。[令牌行编号=1,令牌行偏移量=120,令牌出错=@price]

我在运行我的程序时不断遇到这个错误,我无法找出我做错了什么。我尝试过改变多个方面,但它总是给出相同的错误,只是“tokenline offset=”后面的数字有时会改变

static public void Insert(string _id, string _city, string _state, string _country, int _beds, int _baths, int _price)
{
        try
        {
            connection.Open();
            SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES (@id, @city, @state, @country, @beds, @baths, @price", connection);
            commandInsert.Parameters.AddWithValue("@id", _id);
            commandInsert.Parameters.AddWithValue("@city", _city);
            commandInsert.Parameters.AddWithValue("@state", _state);
            commandInsert.Parameters.AddWithValue("@country", _country);
            commandInsert.Parameters.AddWithValue("@beds", _beds);
            commandInsert.Parameters.AddWithValue("@baths", _baths);
            commandInsert.Parameters.AddWithValue("@price", _price);
            commandInsert.ExecuteNonQuery();
        }
        catch (SqlCeException exception)
        {
            MessageBox.Show(exception.ToString());
        }
        finally
        {
            connection.Close();
        }
}
按钮中的代码单击

private void btn_insert_Click(object sender, EventArgs e)
{
        if (txt_id.Text != "" && txt_city.Text != "" && txt_state.Text != "" && txt_country.Text != "")
        {
            SQLFunctions.Insert(txt_id.Text, txt_city.Text, txt_state.Text, txt_country.Text, int.Parse(txt_beds.Text), int.Parse(txt_baths.Text), int.Parse(txt_Price.Text));
            SQLFunctions.Refresh(this.dataGridView1);
        }
}

我想您忘记在
@price
参数之后关闭
值(
部分

VALUES (@id, @city, @state, @country, @beds, @baths, @price)
还有几件事

  • 用于自动处理连接和命令,而不是手动调用close方法
  • 尽量不要使用
    AddWithValue
    方法。使用
    Add
    方法重载指定参数类型及其大小

我想你忘了在
@price
参数之后关闭
值(
部分

VALUES (@id, @city, @state, @country, @beds, @baths, @price)
还有几件事

  • 用于自动处理连接和命令,而不是手动调用close方法
  • 尽量不要使用
    AddWithValue
    方法。使用
    Add
    方法重载指定参数类型及其大小
试试这个

query="INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES ('@id', '@city', '@state', '@country', '@beds', '@baths', '@price')";
然后用这个替换您的
尝试

 connection.Open();
            SqlCeCommand commandInsert = new SqlCeCommand(query, connection);
            commandInsert.Parameters.AddWithValue("id", _id);
            commandInsert.Parameters.AddWithValue("city", _city);
            commandInsert.Parameters.AddWithValue("state", _state);
            commandInsert.Parameters.AddWithValue("country", _country);
            commandInsert.Parameters.AddWithValue("beds", _beds);
            commandInsert.Parameters.AddWithValue("baths", _baths);
            commandInsert.Parameters.AddWithValue("price", _price);
            commandInsert.ExecuteNonQuery();
试试这个

query="INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES ('@id', '@city', '@state', '@country', '@beds', '@baths', '@price')";
然后用这个替换您的
尝试

 connection.Open();
            SqlCeCommand commandInsert = new SqlCeCommand(query, connection);
            commandInsert.Parameters.AddWithValue("id", _id);
            commandInsert.Parameters.AddWithValue("city", _city);
            commandInsert.Parameters.AddWithValue("state", _state);
            commandInsert.Parameters.AddWithValue("country", _country);
            commandInsert.Parameters.AddWithValue("beds", _beds);
            commandInsert.Parameters.AddWithValue("baths", _baths);
            commandInsert.Parameters.AddWithValue("price", _price);
            commandInsert.ExecuteNonQuery();

我最初使用的是
Add
,但后来它一直用绿色在我的代码下面划线,并说它过时了或什么的。如果使用
AddWithValue
,它就消失了。@Alex Only
Add(String,Object)
重载过时了。您可以改为使用。我最初使用的是
Add
,但后来它一直在我的代码下面加上绿色下划线,并说它过时了,然后使用
AddWithValue
,它就消失了。@Alex Only
Add(String,Object)
重载过时了。您可以使用。