Operator @不存在错误

时间:2015-02-17 06:14:44

标签: c# postgresql

我尝试更新行,但这是问题,因为编译器说"运算符不存在@字符变化"。 谁知道为什么?

p.sql = new NpgsqlCommand("UPDATE users SET us_middlename = @us_middlename, us_country_home = @us_country_home, us_city_home = @us_city_home,  us_type = @us_type, us_org_id = @us_org_id, us_institution_id = @us_institution_id WHERE us_id ='" + id1 + "';", npgSqlConnection);

p.npgSqlCommand1.Parameters.AddWithValue("@us_middlename", user_par.us_middlename); //have character varying value
                            p.npgSqlCommand1.Parameters.AddWithValue("@us_country_home", user_par.us_country_home); //have int value
                            p.npgSqlCommand1.Parameters.AddWithValue("@us_city_home", user_par.us_city_home); //have int value
p.npgSqlCommand1.Parameters.AddWithValue("@us_type", user_par.us_type);//have int value
 p.npgSqlCommand1.Parameters.AddWithValue("@us_org_id", user_par.us_org_id);//have int value
                            p.npgSqlCommand1.Parameters.AddWithValue("@us_institution_id", user_par.us_institution_id);//have int value
count = p.sql.ExecuteNonQuery();        

2 个答案:

答案 0 :(得分:0)

id1之后不要添加分号我还建议您通过参数传递id1

p.sql = new NpgsqlCommand("UPDATE users SET us_middlename = @us_middlename, us_country_home = @us_country_home, us_city_home = @us_city_home,  us_type = @us_type, us_org_id = @us_org_id, us_institution_id = @us_institution_id WHERE us_id ='" + id1 +"'", npgSqlConnection); //Removed ;

答案 1 :(得分:0)

对于Npgsql,您需要在查询中使用:符号作为参数名称:

p.sql = new NpgsqlCommand(
   "UPDATE users " +
   "SET " +
      "us_middlename = :us_middlename, " +
      "us_country_home = :us_country_home, " +
      "us_city_home = :us_city_home, " +
      "us_type = :us_type, " +
      "us_org_id = :us_org_id, " +
      "us_institution_id = :us_institution_id " +
   "WHERE us_id ='" + id1 + "';",
   npgSqlConnection
);

对于参数,您不必添加:(我已经看过它们的示例,如果没有它们,请尝试两者,如果可以的话。)

p.npgSqlCommand1.Parameters.AddWithValue("us_middlename", user_par.us_middlename); //have character varying value
p.npgSqlCommand1.Parameters.AddWithValue("us_country_home", user_par.us_country_home); //have int value
p.npgSqlCommand1.Parameters.AddWithValue("us_city_home", user_par.us_city_home); //have int value
p.npgSqlCommand1.Parameters.AddWithValue("us_type", user_par.us_type);//have int value
p.npgSqlCommand1.Parameters.AddWithValue("us_org_id", user_par.us_org_id);//have int value
p.npgSqlCommand1.Parameters.AddWithValue("us_institution_id", user_par.us_institution_id);//have int value
count = p.sql.ExecuteNonQuery();

您还应该使用id1的参数,但这是一个不同的问题。