使用npgsql调用以字符为参数的函数
我试图使用 Npgsql 调用一个以 CHARACTER 作为参数的函数(存储过程),但它不起作用。如果我声明不带参数或带有 INTEGER 参数的相同函数,我会得到我想要的结果集。当我将参数声明为 CHARACTER 时,它停止工作。怎么了?
这是我的函数的代码:
CREATE OR REPLACE FUNCTION testrefcursor1(in xxx character varying(10)) RETURNS SETOF refcursor AS
$$
DECLARE
ref1 refcursor;
ref2 refcursor;
BEGIN
OPEN ref1 FOR
SELECT * FROM accounts;
RETURN NEXT ref1;
OPEN ref2 FOR
SELECT * FROM accounts;
RETURN NEXT ref2;
RETURN;
END;
$$
LANGUAGE plpgsql;
这是我正在使用的 C# 代码:
var connection = new Npgsql.NpgsqlConnection(connectionString.ConnectionString);
connection.Open();
var trans = connection.BeginTransaction();
var command = new Npgsql.NpgsqlCommand("testrefcursor1", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
var parameter = command.CreateParameter();
parameter.ParameterName = "xxx";
parameter.DbType = System.Data.DbType.String;
parameter.Value = "10";
command.Parameters.Add(parameter);
var da = new Npgsql.NpgsqlDataAdapter(command);
var ds = new System.Data.DataSet();
da.Fill(ds);
trans.Commit();
connection.Close();
我已经尝试将参数声明为 CHARACTER、CHARACTER(10)、CHARACTER VARYING 和 CHARACTER VARYING(10)...
编辑:我不收到任何错误消息,但我没有得到预期的结果集,而是得到一个空结果集,其中有一列与我尝试调用的函数同名。
I am trying to use Npgsql to invoke a function (stored procedure) that takes a CHARACTER as parameter, but it doesn't work. If I declare the same function without parameters, or with an INTEGER parameter, I get the result sets that I want. When I declare the parameter as CHARACTER, it stops working. What is wrong?
Here is the code of my function:
CREATE OR REPLACE FUNCTION testrefcursor1(in xxx character varying(10)) RETURNS SETOF refcursor AS
$
DECLARE
ref1 refcursor;
ref2 refcursor;
BEGIN
OPEN ref1 FOR
SELECT * FROM accounts;
RETURN NEXT ref1;
OPEN ref2 FOR
SELECT * FROM accounts;
RETURN NEXT ref2;
RETURN;
END;
$
LANGUAGE plpgsql;
And here is the C# code that I am using:
var connection = new Npgsql.NpgsqlConnection(connectionString.ConnectionString);
connection.Open();
var trans = connection.BeginTransaction();
var command = new Npgsql.NpgsqlCommand("testrefcursor1", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
var parameter = command.CreateParameter();
parameter.ParameterName = "xxx";
parameter.DbType = System.Data.DbType.String;
parameter.Value = "10";
command.Parameters.Add(parameter);
var da = new Npgsql.NpgsqlDataAdapter(command);
var ds = new System.Data.DataSet();
da.Fill(ds);
trans.Commit();
connection.Close();
I already tried declaring the parameter as CHARACTER, CHARACTER(10), CHARACTER VARYING and CHARACTER VARYING(10)...
EDIT: I don't get any error message, but instead of getting the expected result set, I get an empty result set with a single column that has the same name as the function I am trying to call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在将 Unicode 参数传递给 ASCII 参数。
将此行更改
为
:通常,Postgres 的
varchar
列采用 Unicode 格式,前提是启用了数据库上的 Unicode 选项 (请参阅此处)。我的猜测是事实并非如此,并且您的参数无法将自身转换为要通过函数传递的正确类型。You're passing a Unicode argument to an ASCII parameter.
Change this line:
To:
Generally, Postgres's
varchar
columns are in Unicode, provided that the Unicode option on the database is enabled (see here). My guess is that it's not, and your parameter is unable to convert itself into the correct type to be passed through the function.您使用的是哪个 Npgsql 版本?
另外,您可以使用 NpgsqlDbType 指定参数类型吗?有时映射不准确,Npgsql 无法找到您尝试使用的函数并且无法使其工作。
Npgsql 尝试找到函数名称和参数类型的精确匹配。 DbString 匹配文本参数类型。您介意尝试一下并将参数类型更改为文本吗?
我希望它有帮助。
Which Npgsql version are you using?
Also, can you specify the parameter type using NpgsqlDbType? Sometimes the mapping isn't exactly and Npgsql can't find the function you are trying to use and can't make it work.
Npgsql tries to find an exact match of function name and parameter types. DbString matches text parameter types. Would you mind to give it a try and change your parameter type to text?
I hope it helps.
不确定,这是否与您的问题有关,但昨天我偶然发现了 事实 PostgreSQL 有一个与 char(1) 类型不同的“单字节内部类型”char。或许大家对于这些还有一些困惑?
Not sure, if this has to do with your problem, but yesterday I stumbled across the fact that PostgreSQL has a "single-byte internal type" char that is different from the type char(1). Maybe there is some confusion about these?
我尝试了以下方法(与您的方法类似):
这对我来说效果很好,并且我得到了正确的数据表。
I tried with the below approach (which is similar to yours):
This is working fine for me and I'm getting proper DataTable.