【问题标题】:Npgsql.NpgsqlException: ERROR: 42601: syntax error at or near "where"Npgsql.NpgsqlException:错误:42601:“where”或附近的语法错误
【发布时间】:2012-12-04 14:32:51
【问题描述】:

我在 asp.net 中有以下 web 服务

//setup profile
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void SetProfile(string userName, string firstName, string lastName, string imageUrl)
    {
        //create and open connection
        NpgsqlConnection profileConnection = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["PrevueConnString"].ToString());
        profileConnection.Open();

        //create query and command
        string query = "INSERT into \"Users\" (\"FirstName\", \"LastName\", \"ImageUrl\") values(:fname, :lname, :imageUrl) where \"UserName\" = :user";
        NpgsqlCommand profileCommand = new NpgsqlCommand(query, profileConnection);

        profileCommand.Parameters.Add(new NpgsqlParameter("user", DbType.String));
        profileCommand.Parameters.Add(new NpgsqlParameter("fname", DbType.String));
        profileCommand.Parameters.Add(new NpgsqlParameter("lname", DbType.String));
        profileCommand.Parameters.Add(new NpgsqlParameter("imageUrl", DbType.String));

        profileCommand.Parameters[0].Value = userName;
        profileCommand.Parameters[1].Value = firstName;
        profileCommand.Parameters[2].Value = lastName;
        profileCommand.Parameters[3].Value = imageUrl;

        int result = profileCommand.ExecuteNonQuery();

        profileCommand.Dispose();
        profileConnection.Close();

        string json = new JavaScriptSerializer().Serialize(result);
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.Flush();
        Context.Response.Write(json);
    }

在调用网络服务时,我收到以下错误:

Npgsql.NpgsqlException: ERROR: 42601: "where" 或附近的语法错误

【问题讨论】:

  • 请显示由 nPgSQL 生成并发送到数据库的最终 SQL。您可以从 PostgreSQL 服务器日志中获取此信息。您可能需要设置log_statement = 'all'
  • 嘿 Craig,我想我发现了错误,我使用了带有 Insert 命令的 'Where' 子句,将其更改为 Update,现在一切顺利... :) 感谢您的帮助.. !!
  • 哇...我很尴尬,我没有发现这一点。对不起。

标签: asp.net web-services postgresql npgsql postgresql-9.2


【解决方案1】:

我想我发现了错误,我将“Where”子句与插入命令一起使用,将其更改为更新,现在一切顺利... :) 感谢您的帮助..!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2021-02-28
    • 1970-01-01
    • 2020-06-11
    • 2021-01-17
    相关资源
    最近更新 更多