我是postgres数据访问api的新手/新手。我已经在oracle,sql server上做了一些工作,并尝试做我用dbms做的事情。使用非常简单1)存储过程aka输入参数的函数2)返回或更多ref游标3)使用ent lib包装器使用npgsql提供程序/数据库与它4)做一个数据适配器填充并运行一些游标解除引用的问题..它出现虽然我在一个tran .. 5)我只是想得到一些简单的工作示例使用最新的npgsql提供程序..
这是我的功能
CREATE OR REPLACE FUNCTION public.geterrorcategories(
v_organizationid integer)
RETURNS refcursor
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE cv_1 refcursor;
BEGIN
open cv_1 for
SELECT errorCategoryId, name, bitFlag
FROM ErrorCategories
ORDER BY name;
RETURN cv_1;
END;
$BODY$;
使用企业lib api / wrapper的代码如下。
/// <summary>
/// Executes GetErrorCategories in case of SQL Server or GetErrorCategories for Oracle
/// </summary>
public static DataTable GetErrorCategoriesAsDataTable(string dbKey ,int? ORGANIZATIONID)
{
DataTable tbl = new DataTable();
Database db = Helper.GetDatabase(dbKey);
using (DbConnection con = db.CreateConnection()){
con.Open();
var tran = con.BeginTransaction();
using (DbCommand cmd = con.CreateCommand()){
cmd.Transaction = tran;
BuildGetErrorCategoriesCommand(db, cmd ,ORGANIZATIONID);
cmd.CommandText = "GetErrorCategories";
try {
Helper.FillDataTable(tbl, db, cmd);
con.Close();
} catch (DALException ) {
throw;
}
}
}
return tbl;
}
该命令构建如下。
private static void BuildGetErrorCategoriesCommand(Database db, DbCommand cmd ,int? ORGANIZATIONID){
Helper.InitializeCommand(cmd, 300, "GetErrorCategories");
db.AddReturnValueParameter(cmd);
db.AddInParameter(cmd, "organizationId", DbType.Int32, ORGANIZATIONID);
db.AddCursorOutParameter(cmd, "CV_1");
}
我没有收到任何错误。我只返回1行,我认为这是un_named_portal_1或者其他东西,但不是我的表中的结果,我的查询返回
令人沮丧的是,我希望尽可能保持我的应用程序代码相同,但希望在运行时切换提供程序。我正在使用为npgsql创建的调整后的'ent lib'贡献数据库。
希望这有助于指出我正确的寻找区域..
上面没有理由声明你的PostgreSQL函数返回一个游标 - 你只需返回一个表,参见the PostgreSQL docs for more info。
Npgsql最初有一个功能,它自动“取消引用”从函数返回的游标,但这已被删除。有关这方面的更多信息,请参阅this issue(警告,它很长......)。有些人要求退回该功能。