sql中引用的参数是抛出的out参数异常

时间:2018-05-13 16:26:01

标签: postgresql npgsql

我有一个名为raw ad-hoc查询并使用输出参数执行。我相信,我正在向命令对象添加输入和输出参数。我试图理解Npgsql中输出参数的解析以及它失败的原因。任何想法..我试图在这里提供一些信息..如果您能提供帮助或需要其他信息,请告诉我。我认为这应该是一个简单的用例来插入一些数据并使用params从命名查询中获取一些标量返回值

的Postgres

BEGIN

 SELECT nextval('Role_seq') into :v_roleId;

  INSERT INTO Role (roleId, organizationId, name, notes, locked, roleTypeId, rightsFlags)
  VALUES (:v_roleId, :v_organizationId, :v_name, :v_notes, :v_locked, :v_roleTypeId, :v_rightsFlags);

END;

SQL Server

    INSERT INTO Role (organizationId, name, notes, locked, roleTypeId, rightsFlags)
VALUES (@organizationId, @name, @notes, @locked, @roleTypeId, @rightsFlags)
SELECT @roleId = SCOPE_IDENTITY()

的Oracle

BEGIN
  SELECT Role_roleId_SEQ.NEXTVAL into :v_roleId FROM DUAL;

  INSERT INTO Role (roleId, organizationId, name, notes, locked, roleTypeId, rightsFlags)
  VALUES (:v_roleId, :v_organizationId, :v_name, :v_notes, :v_locked, :v_roleTypeId, :v_rightsFlags);
END;

我正确绑定了所有参数,并且此代码适用于除Postgres之外的所有平台(提供程序),其中某些查询解析失败。这是我如何添加参数。

   dsh.AddNQParameter(cmd, "roleId", ParameterDirection.Output, (object)DBNull.Value, "Int", "Int32", "Integer");
            dsh.AddNQParameter(cmd, "organizationId", ParameterDirection.Input, organizationId ?? (object)DBNull.Value, "Int", "Int32", "Integer");
            dsh.AddNQParameter(cmd, "name", ParameterDirection.Input, name ?? (object)DBNull.Value, "VarChar", "Varchar2", "Varchar");
            dsh.AddNQParameter(cmd, "notes", ParameterDirection.Input, notes ?? (object)DBNull.Value, "VarChar", "Varchar2", "Varchar");
            dsh.AddNQParameter(cmd, "locked", ParameterDirection.Input, locked ?? (object)DBNull.Value, "Bit", "Byte", "Boolean");
            dsh.AddNQParameter(cmd, "roleTypeId", ParameterDirection.Input, roleTypeId ?? (object)DBNull.Value, "Int", "Int32", "Integer");
            dsh.AddNQParameter(cmd, "rightsFlags", ParameterDirection.Input, rightsFlags ?? (object)DBNull.Value, "Image", "Blob", "Bytea");

Postgres的堆栈跟踪

Result StackTrace:  
at Npgsql.SqlQueryParser.ParseRawQuery(String sql, Boolean standardConformantStrings, NpgsqlParameterCollection parameters, List`1 statements)
   at Npgsql.NpgsqlCommand.ProcessRawQuery()
   at Npgsql.NpgsqlCommand.<Execute>d__71.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlCommand.<ExecuteNonQuery>d__84.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at LandisGyr.Data.Helper.ExecuteNonQueryReturnInt(DbCommand cmd, String name) in D:\tfs\cc\Command Center\Components\LGDALGenerator\Main\LG.Data.Core\Foundation\Helper.cs:line 76
   at DAL_Generator_Test.Data.NamedQueries.Test.NamedQueriesTest.InsRole(DbCommand cmd, Nullable`1 organizationId, String name, String notes, Nullable`1 locked, Nullable`1 roleTypeId, Byte[] rightsFlags, Nullable`1& roleId) in D:\tfs\cc\Command Center\Components\LGDALGenerator\Main\DAL Generator Test\Data\NamedQueries\NamedQueries.Test.Designer.cs:line 985
   at DAL_Generator_Test.SqlServerTests.NamedQueriesPostgresTests.Execute_NonQuery_Test_Using_DbCommand() in D:\tfs\cc\Command Center\Components\LGDALGenerator\Main\DAL Generator Test\PostgresTests\NamedQueriesPostgresTests.cs:line 90
Result Message: 
Test method DAL_Generator_Test.SqlServerTests.NamedQueriesPostgresTests.Execute_NonQuery_Test_Using_DbCommand threw exception: 
System.Exception: Parameter ':v_roleId' referenced in SQL but is an out-only parameter

代码示例以重现问题

using Npgsql;
using NpgsqlTypes;
using System;
using System.Configuration;
using System.Data.Common;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectString = ConfigurationManager.ConnectionStrings["PostgresTest"].ConnectionString;

            // create a table as follows
            /*
             * CREATE TABLE  role
(
    roleid integer NOT NULL DEFAULT nextval('role_seq'::regclass),
    name character varying(50) COLLATE pg_catalog."default",
    notes character varying(255) COLLATE pg_catalog."default",
    organizationid integer NOT NULL,
    roletypeid integer NOT NULL DEFAULT 0,
    locked boolean NOT NULL DEFAULT false,
    rightsflags bytea
)
             */
            using (NpgsqlConnection con = new NpgsqlConnection(connectString))
            {

                con.Open();

                using (DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = @"BEGIN

 SELECT nextval('Role_seq') into: v_roleId;

                    INSERT INTO Role(roleId, organizationId, name, notes, locked, roleTypeId, rightsFlags)
  VALUES(:v_roleId, :v_organizationId, :v_name, :v_notes, :v_locked, :v_roleTypeId, :v_rightsFlags);

                    END;";

                    var roleIdParam = new NpgsqlParameter(":v_roleId", NpgsqlDbType.Integer);
                    roleIdParam.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(roleIdParam);

                    var orgParam = new NpgsqlParameter(":v_organizationId", NpgsqlDbType.Integer);
                    orgParam.Direction = System.Data.ParameterDirection.Input;
                    orgParam.Value = 1;
                    cmd.Parameters.Add(orgParam);

                    var nameParam = new NpgsqlParameter(":v_name", NpgsqlDbType.Varchar);
                    nameParam.Direction = System.Data.ParameterDirection.Input;
                    nameParam.Value = "test role";
                    cmd.Parameters.Add(nameParam);

                    var lockedParam = new NpgsqlParameter(":v_locked", NpgsqlDbType.Boolean);
                    lockedParam.Direction = System.Data.ParameterDirection.Input;
                    lockedParam.Value = false;
                    cmd.Parameters.Add(lockedParam);


                    var roleTypeIdParam = new NpgsqlParameter(":v_roleTypeId", NpgsqlDbType.Integer);
                    roleTypeIdParam.Direction = System.Data.ParameterDirection.Input;
                    roleTypeIdParam.Value = 1;
                    cmd.Parameters.Add(roleTypeIdParam);


                    var rightsFlagsParam = new NpgsqlParameter(":v_rightsFlags", NpgsqlDbType.Bytea);
                    rightsFlagsParam.Direction = System.Data.ParameterDirection.Input;
                    rightsFlagsParam.Value = DBNull.Value;
                    cmd.Parameters.Add(rightsFlagsParam);


                    cmd.ExecuteNonQuery();


                    object roleId = cmd.Parameters[":v_roleId"].Value;

                    Console.WriteLine($"role id is {roleId}");

                    Console.WriteLine("Press any key to continue");

                    Console.ReadLine();

                }
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我已经阅读了有关“输入/输出参数”的文档。 https://www.npgsql.org/doc/basic-usage.html

在非常相似的情况下,我进行了一个测试,返回了序列的值。

请注意,您没有在SQL语句中设置输出参数。 INSERT INTO x RETURNING x.roleId INTO:roleId

示例代码

using Npgsql;
using NpgsqlTypes;
using System;
using System.Configuration;
using System.Data.Common;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectString = ConfigurationManager.ConnectionStrings["PostgresTest"].ConnectionString;

            using (NpgsqlConnection con = new NpgsqlConnection(connectString))
            {

                con.Open();

                using (DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = @"INSERT INTO Role(roleId, organizationId, name, notes, locked, roleTypeId, rightsFlags)
  VALUES(nextval('Role_seq'), :v_organizationId, :v_name, :v_notes, :v_locked, :v_roleTypeId, :v_rightsFlags) RETURNING roleId";

                    var orgParam = new NpgsqlParameter(":v_organizationId", NpgsqlDbType.Integer);
                    orgParam.Direction = System.Data.ParameterDirection.Input;
                    orgParam.Value = 1;
                    cmd.Parameters.Add(orgParam);

                    var nameParam = new NpgsqlParameter(":v_name", NpgsqlDbType.Varchar);
                    nameParam.Direction = System.Data.ParameterDirection.Input;
                    nameParam.Value = "test role";
                    cmd.Parameters.Add(nameParam);

                    var lockedParam = new NpgsqlParameter(":v_locked", NpgsqlDbType.Boolean);
                    lockedParam.Direction = System.Data.ParameterDirection.Input;
                    lockedParam.Value = false;
                    cmd.Parameters.Add(lockedParam);


                    var roleTypeIdParam = new NpgsqlParameter(":v_roleTypeId", NpgsqlDbType.Integer);
                    roleTypeIdParam.Direction = System.Data.ParameterDirection.Input;
                    roleTypeIdParam.Value = 1;
                    cmd.Parameters.Add(roleTypeIdParam);


                    var rightsFlagsParam = new NpgsqlParameter(":v_rightsFlags", NpgsqlDbType.Bytea);
                    rightsFlagsParam.Direction = System.Data.ParameterDirection.Input;
                    rightsFlagsParam.Value = DBNull.Value;
                    cmd.Parameters.Add(rightsFlagsParam);


                    var roleIdParam = new NpgsqlParameter("Returning_roleIdParam", NpgsqlDbType.Integer);
                    roleIdParam.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(roleIdParam);

                    cmd.ExecuteNonQuery();


                    object roleId = cmd.Parameters["Returning_roleIdParam"].Value;

                    Console.WriteLine($"role id is {roleId}");

                    Console.WriteLine("Press any key to continue");

                    Console.ReadLine();

                }
            }

        }
    }
}