C# 和 PostgreSQL

标签 c# .net sql postgresql ado.net

谁能给我一个使用从 PLSQL 返回到 C# 代码的游标的工作示例?

我找到了很多展示如何用返回数据填充 dataSet 的示例,但我找不到如何将游标与 DataReader 一起使用,因此我有{未命名的门户}

NpgsqlTransaction tr = (NpgsqlTransaction) Connection.BeginTransaction();
NpgsqlCommand cursCmd = new NpgsqlCommand("someStoredProcedure(:inRadius)", (NpgsqlConnection) Connection);
cursCmd.Transaction = tr;
NpgsqlParameter rf = new NpgsqlParameter("ref", NpgsqlTypes.NpgsqlDbType.Refcursor);
rf.Direction = ParameterDirection.InputOutput;
cursCmd.Parameters.Add(rf);

我必须添加它才能正确使用 NpgsqlDataReader myReader;:

tr.Commit();

当我在 sql 命令后写 fetch 时,它有效但不合适。

最佳答案

供您引用:

/// <summary>
/// Get data from the returning refcursor of postgresql function
/// </summary>
/// <param name="FunctionName">Function name of postgresql</param>
/// <param name="Parameters">parameters to pass to the postgresql function</param>
/// <param name="ErrorOccured">out bool parameter to check if it occured error</param>
/// <returns></returns>
public List<DataTable> GetRefCursorData(string FunctionName, List<object> Parameters, out bool ErrorOccured)
{ 
    string connectstring = ""; //your connectstring here
    List<DataTable >  dtRtn =new List<DataTable>();
    NpgsqlConnection connection = null;
    NpgsqlTransaction transaction = null;
    NpgsqlCommand command = null;            
    try
    {
        connection = new NpgsqlConnection(connectstring);
        transaction = connection.BeginTransaction();
        command = new NpgsqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = FunctionName;
        command.Transaction = transaction;
        //
        if (Parameters != null)
        {
            foreach (object item in Parameters)
            {
                NpgsqlParameter parameter = new NpgsqlParameter();
                parameter.Direction = ParameterDirection.Input;
                parameter.Value = item;
                command.Parameters.Add(parameter);
            }
        }
        //
        NpgsqlDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
            DataTable dt = new DataTable();
            command = new NpgsqlCommand("FETCH ALL IN " + "\"" + dr[0].ToString() + "\"", Connection); //use plpgsql fetch command to get data back
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
            da.Fill(dt);
            dtRtn.Add(dt); //all the data will save in the List<DataTable> ,no matter the connection is closed or returned multiple refcursors
        }                
        ErrorOccured = false;
        transaction.Commit();
    }
    catch
    { 
        //error handling ...
        ErrorOccured = true;
        if (transaction != null) transaction.Rollback();
    }
    finally
    {
        if (connection != null) connection.Close();
    }
    return dtRtn;
}

关于C# 和 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2037000/

相关文章:

c# - int 不是我尝试分配的内容

c# - 如何使用 MongoDB 将动态 JSON 属性发布到 C# ASP.Net Core Web API?

c# - 正则表达式 - 如果存在捕获的组,则条件替换

java - JDBC 中缺少 dll

MySQL:根据另一个列值在列中选择唯一值

c# - 为什么 .NET Native 上的 Raygun 会导致我的应用程序崩溃?

c# - app.config 中的 FTP 连接

c# - URL重写+输出缓存

c# - 使用 c# .net 逐行读取 excel 数据

android - 如何在 html5、phonegap 和 jQuery 中创建进度条