使用npgsql调用以字符为参数的函数

发布于 2024-08-02 12:42:49 字数 1278 浏览 3 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肩上的翅膀 2024-08-09 12:42:49

您正在将 Unicode 参数传递给 ASCII 参数。

将此行更改

parameter.DbType = System.Data.DbType.String;

parameter.DbType = System.Data.DbType.AnsiString;

:通常,Postgres 的 varchar 列采用 Unicode 格式,前提是启用了数据库上的 Unicode 选项 (请参阅此处)。我的猜测是事实并非如此,并且您的参数无法将自身转换为要通过函数传递的正确类型。

You're passing a Unicode argument to an ASCII parameter.

Change this line:

parameter.DbType = System.Data.DbType.String;

To:

parameter.DbType = System.Data.DbType.AnsiString;

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.

江湖彼岸 2024-08-09 12:42:49

您使用的是哪个 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.

最终幸福 2024-08-09 12:42:49

不确定,这是否与您的问题有关,但昨天我偶然发现了 事实 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?

不即不离 2024-08-09 12:42:49

我尝试了以下方法(与您的方法类似):

using (NpgsqlConnection connection = new NpgsqlConnection(< connectionString >))
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    connection.Open();
    NpgsqlCommand cmd = new NpgsqlCommand(< name of your SP >, connection);
    cmd.CommandType = CommandType.StoredProcedure;
    var param = cmd.CreateParameter();
    param.ParameterName = < exact parameterName you used in your SP>;
    param.DbType = System.Data.DbType.AnsiString;
    param.Value = < parameter value >;
    cmd.Parameters.Add(param);
    NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
    adapter.Fill(ds);
    dt = ds.Tables[0];
}

这对我来说效果很好,并且我得到了正确的数据表。

I tried with the below approach (which is similar to yours):

using (NpgsqlConnection connection = new NpgsqlConnection(< connectionString >))
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    connection.Open();
    NpgsqlCommand cmd = new NpgsqlCommand(< name of your SP >, connection);
    cmd.CommandType = CommandType.StoredProcedure;
    var param = cmd.CreateParameter();
    param.ParameterName = < exact parameterName you used in your SP>;
    param.DbType = System.Data.DbType.AnsiString;
    param.Value = < parameter value >;
    cmd.Parameters.Add(param);
    NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
    adapter.Fill(ds);
    dt = ds.Tables[0];
}

This is working fine for me and I'm getting proper DataTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文