我在Ubuntu上使用.NET Npgsql
库连接到PostgreSQL dbms。
在我的数据库中,我有一个声明的FUNCTION“user_plan()”,它返回一个SETOF SELECT
查询结果:
CREATE FUNCTION user_plan(varchar(20), int) RETURNS SETOF user_plan_type AS
$$
SELECT pr.participant, t.talk_id, t.start_time, t.description, t.room
FROM talks t JOIN participant_registrations pr USING(event)
WHERE pr.participant=$1
ORDER BY 3
LIMIT $2;
$$ LANGUAGE SQL;
当我直接从psql环境测试时,一切都按预期进行。但是当我使用Npgsql
库以这种方式调用FUNCTION时:
using (var query = new NpgsqlCommand())
{
query.Connection = conn;
query.CommandText = "SELECT user_plan(@user, @limit::int)";
query.Parameters.AddWithValue("user", login);
query.Parameters.AddWithValue("limit", limit);
using (var dr = query.ExecuteReader()) {
while(dr.Read()){
Console.Write("{0} \n", dr[0]);
}
}
}
或者这样:
using (var query = new NpgsqlCommand())
{
query.Connection = conn;
query.CommandText = "SELECT user_plan(@user, @limit::int)";
query.Parameters.AddWithValue("user", login);
query.Parameters.AddWithValue("limit", limit);
using (var dr = query.ExecuteReader()) {
while(dr.Read()){
Console.WriteLine(dr.GetString(0));
}
}
}
我收到以下错误:
System.Collections.ListDictionaryInternal:
The field 'user_plan' has a type currently unknown to Npgsql (OID 16642).
You can retrieve it as a string by marking it as unknown, please see the FAQ.
我只是想编写控制user_plan()
查询的结果集。我该怎么办?
答案 0 :(得分:0)
问题出在query
本身。它返回函数类型,而不是返回值。
而不是:
query.CommandText = "SELECT user_plan(@user, @limit::int)";
应该是:
query.CommandText = "SELECT * FROM user_plan(@user, @limit::int)";