如何从 ASP.Net 核心中的 PostgreSQL 函数获取数据

How to get data from PostgreSQL function in ASP.Net core

我是 asp.net 核心的初学者。我的 posgres 中有一个名为“GetDriverStatusReport”的工作函数将 return 一些数据。我想在我的 Asp.Net 核心应用程序中检索这些值。我尝试对此进行研究,但找不到合适的解决方案。有人可以告诉我在 Asp.net Core 中调用存储过程的正确性吗?提前致谢

这是我的函数

CREATE OR REPLACE FUNCTION GetDriverStatusReport(ref refcursor) RETURNS refcursor AS $$
BEGIN
  OPEN ref FOR (WITH union_tbl AS(
SELECT
   DR."DriverId" AS "Driver Id",
   DR."DriverName" As "Driver Name",                
   0 as "RiderIncome",
   0 as "Payable To Rider",
   0 as "Recievable Balance",
   DR."Amount" as "Amount Paid"
FROM "DriverPaymentReferences" as DR            
UNION ALL
SELECT
   DF."DriverId" AS "Driver Id",
   DF."DriverName" As "Driver Name",
   DF."RiderIncome" as "RiderIncome",
   0 as "Payable To Rider",
   0 as "Recievable Balance",
   0 as "Amount Paid"
FROM "DeliveryFinances" as DF 
UNION ALL
SELECT
   TF."DriverId" AS "Driver Id",
   TF."DriverName" As "Driver Name",
   0 as "RiderIncome",
   TF."PayableToRider" as "Payable To Rider",
   TF."RecievableBalance" as "Recievable Balance",
   0 as "Amount Paid"
FROM "TaxiFinances" as TF)
Select 
   "Driver Id", 
   "Driver Name",
   case when 
     ((Sum("Recievable Balance") - SUM("Payable To Rider")) -Sum("RiderIncome")- 
   SUM("Amount Paid")) > 0
   then 'CR'
   else 'DR' end as "Cash/Debit",
   ((Sum("Recievable Balance") - SUM("Payable To Rider")) -Sum("RiderIncome")- 
   SUM("Amount Paid")) AS "Amount"
FROM union_tbl
GROUP BY "Driver Id","Driver Name" );
  RETURN ref;                                                      
END;
$$ LANGUAGE plpgsql;

嗯,你可以这样试试,

假设我有一个名为 SP_GetAllMSManager 的存储过程,就像这样

PG SQL

CREATE OR REPLACE FUNCTION GetWSManager() 
AS $$

BEGIN
      SELECT DISTINCT  WSManager FROM WsEmployee ORDER BY WSManager 
      COMMIT;      
END;
$$ LANGUAGE plpgsql;

C#ASP.NET核心:

NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;Password=pwd;Database=postgres;");

conn.Open();

// Passing PostGre SQL Function Name
NpgsqlCommand command = new NpgsqlCommand("EXE GetEmployeePrintInfo", conn);

// Execute the query and obtain a result set
NpgsqlDataReader reader = command.ExecuteReader();

// Reading from the database rows
List<string> listOfManager = new List<string>();

while (reader.Read())
{
    string WSManager = reader["WSManager"].ToString(); // Remember Type Casting is required here it has to be according to database column data type
    listOfManager.Add(WSManager);
}
reader.Close();

command.Dispose();
conn.Close();

输出:

如果上下文相同:

如果你想使用相同的上下文,那么你可以尝试这种方式:

C#ASP.NET核心:

  var printJobList =  _context.PrinterJobs
                     .FromSql("EXECUTE GetEmployeePrintInfo")
                     .ToList();

注意:这属于_context.PrinterJobs,意思是store procedure只从PrinterJobstable获取数据。所以我可以按照上面的方式做。但是,如果您的存储过程包含来自 join 的其他实体数据,那么第一种方法会很方便。你可以看看official document here

PG SQL:

CREATE OR REPLACE FUNCTION GetEmployeePrintInfo() 
AS $$
BEGIN
       SELECT DISTINCT PrinterId ,PrinterName,PrintedBy,TotalPrint FROM PrintJob 
       COMMIT;     
END;
$$ LANGUAGE plpgsql;

希望对您有所帮助。