C#错误解析查询
C# error parsing to query
System.Data.SqlServerCeException (0x80004005): There was an error parsing to the query. [Token line number = 1, Token line offset = 120, Token in error = @price ]
当我的程序 运行 时,我一直收到此错误,我无法弄清楚我做错了什么。我试过改变很多东西,它总是给出同样的错误,只是 "Token line 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();
}
}
buttonclick 中的代码
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(
部分。
VALUES (@id, @city, @state, @country, @beds, @baths, @price)
还有一些事情;
- 使用
using
statement 自动处理您的连接和命令,而不是手动调用关闭方法。
- 尽量不要使用
AddWithValue
方法。 It may generate unexpected and surprising results sometimes。使用 Add
方法重载来指定您的参数类型及其大小。
试试这个
query="INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES ('@id', '@city', '@state', '@country', '@beds', '@baths', '@price')";
并用这个
替换你的try
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();
System.Data.SqlServerCeException (0x80004005): There was an error parsing to the query. [Token line number = 1, Token line offset = 120, Token in error = @price ]
当我的程序 运行 时,我一直收到此错误,我无法弄清楚我做错了什么。我试过改变很多东西,它总是给出同样的错误,只是 "Token line 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();
}
}
buttonclick 中的代码
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(
部分。
VALUES (@id, @city, @state, @country, @beds, @baths, @price)
还有一些事情;
- 使用
using
statement 自动处理您的连接和命令,而不是手动调用关闭方法。 - 尽量不要使用
AddWithValue
方法。 It may generate unexpected and surprising results sometimes。使用Add
方法重载来指定您的参数类型及其大小。
试试这个
query="INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES ('@id', '@city', '@state', '@country', '@beds', '@baths', '@price')";
并用这个
替换你的try
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();