Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关键字';其中';_C#_Sql Server 2008 - Fatal编程技术网

C# 关键字';其中';

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

问题出在命令where中:关键字where附近的语法不正确。
救命啊

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 ;