【发布时间】:2018-05-13 16:26:00
【问题描述】:
我有一个命名的原始临时查询并使用输出参数执行。我相信我正在正确地将输入和输出参数添加到命令对象。我试图了解 Npgsql 中输出参数的解析以及它失败的原因。任何想法..我试图在这里提供一些信息..让我知道您是否可以提供帮助或需要其他信息。我认为这应该是一个简单的用例,用于插入一些数据并使用 out 参数从命名查询中获取一些标量返回值
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 服务器
INSERT INTO Role (organizationId, name, notes, locked, roleTypeId, rightsFlags)
VALUES (@organizationId, @name, @notes, @locked, @roleTypeId, @rightsFlags)
SELECT @roleId = SCOPE_IDENTITY()
甲骨文
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();
}
}
}
}
}
【问题讨论】:
-
呃,我可以在 SQL 语句中看到
:v_roleId,但在您的代码填充参数中看不到(那里只有roleId)... -
是的。我有一些代码,其中 :v_ 参数前缀已添加如下 var npgsqlParam = new NpgsqlParameter(string.Format(":{0}{1}", paramName.StartsWith(PostgresParameterPrefix, StringComparison.OrdinalIgnoreCase) ? "" : PostgresParameterPrefix, paramName.ToLower()), dbSpecificTypesMap[dbSpecificType]); npgsqlParam.Direction = 方向; npgsqlParam.Value = 参数值; ocmd.Parameters.Add(npgsqlParam);
-
:v_ 是我的 postgres 提供程序抽象添加的参数前缀,这就是为什么你在那里看不到它。
-
这不清楚,因为您显示的相关代码太少。这些以
:开头的东西是什么? -
我明白了。让我编写一个小示例应用程序并发布该代码。此代码将删除围绕 Npgsql 的任何框架抽象。现在请把 : 作为一些参数前缀来表示它是一个可绑定的参数
标签: postgresql npgsql