C# 关键字';其中';
问题出在命令where中:关键字where附近的语法不正确。C# 关键字';其中';,c#,sql-server-2008,C#,Sql Server 2008,问题出在命令where中:关键字where附近的语法不正确。 救命啊 在insert语句中不能有where子句。就这些。如果要插入,请删除where子句。如果需要更新符合条件的记录,请不要使用insert,而是使用update 此外,如果您对查询结果不感兴趣,请不要使用ExecuteReader,而是ExecuteOnQueryThorsten答案很清楚,我只是为每种情况添加代码: public bool location() { string OUI = "OUI"; S
救命啊 在
insert
语句中不能有where
子句。就这些。如果要插入,请删除where
子句。如果需要更新符合条件的记录,请不要使用insert
,而是使用update
此外,如果您对查询结果不感兴趣,请不要使用
ExecuteReader
,而是ExecuteOnQuery
Thorsten答案很清楚,我只是为每种情况添加代码:
public bool location()
{
string OUI = "OUI";
SqlConnection con = new SqlConnection(@"Data Source=WIN-218NC1F1FE2\SQLEXPRESS;Initial Catalog=projet;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select max(id_reservation) from reservation");
cmd.Connection = con;
Int32 maxId = (Int32)cmd.ExecuteScalar();
string v = Convert.ToString(maxId);
//correct
SqlCommand q = new SqlCommand("insert into reservation(location) values('" + OUI + "') where id_reservation ='"+ maxId + "'", con);
SqlDataReader da = q.ExecuteReader();
return true ;
}
事实并非如此。如果insert语句使用Select,那么可以使用where子句修改Select。这是完全不同的。WHERE子句属于SELECT而不是INSERT语句。当然,在SELECT中可以有WHERE子句,但在INSERT中不能有问题中建议的WHERE子句。
SqlConnection con = new SqlConnection(@"Data Source=WIN-218NC1F1FE2\SQLEXPRESS;Initial Catalog=projet;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select max(id_reservation) from reservation");
cmd.Connection = con;
Int32 maxId = (Int32)cmd.ExecuteScalar();
string ID=maxId.TOString();
//correct
/////INSERT
SqlCommand q = new SqlCommand("insert into reservation(location) values(@location,@ID)", con);
q.Parameters.AddWithValue( "@location",OUI);
q.Parameters.AddWithValue("@ID",ID);
q.ExecuteNonQuery();
return true ;
////////UPDATE
SqlCommand q = new SqlCommand("update reservation set location=@location where id_reservation =@ID", con);
q.Parameters.AddWithValue( "@location",OUI);
q.Parameters.AddWithValue("@ID",ID);
q.ExecuteNonQuery();
return true ;