Return DataSet from Npgsql with multiple result set as Tables
clsPSQLConnectionCreator dbHelper = new clsPSQLConnectionCreator();
NpgsqlCommand sc = dbHelper.GetSqlQueryCommand("select * from systemtracker_dbo.pr_get_dataset('parameter1','parameter2','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1');FETCH ALL IN t1; FETCH ALL IN t2;FETCH ALL IN t3;FETCH ALL IN t4;FETCH ALL IN t5;FETCH ALL IN t6;FETCH ALL IN t7;FETCH ALL IN t8;");
DataSet ds = dbHelper.LoadDataSet(sc);
return ds;
public NpgsqlCommand GetSqlQueryCommand(string query)
{
return PrepareCommand(CommandType.Text, query);
}
private NpgsqlCommand PrepareCommand(CommandType commandType, string commandText)
{
if (connection == null)
{
connection = new NpgsqlConnection(this.connectionString);
}
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
{
connection.Open();
}
NpgsqlCommand command = new NpgsqlCommand(commandText, connection);
command.CommandType = commandType;
return command;
}
public DataSet LoadDataSet(NpgsqlCommand command, string[] tableNames = null)
{
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(command))
{
using (DataSet ds = new DataSet())
{
da.Fill(ds);
return ds;
}
}
}
NpgsqlCommand sc = dbHelper.GetSqlQueryCommand("select * from systemtracker_dbo.pr_get_dataset('parameter1','parameter2','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1','ref_cursor1');FETCH ALL IN t1; FETCH ALL IN t2;FETCH ALL IN t3;FETCH ALL IN t4;FETCH ALL IN t5;FETCH ALL IN t6;FETCH ALL IN t7;FETCH ALL IN t8;");
DataSet ds = dbHelper.LoadDataSet(sc);
return ds;
GetSqlQueryCommand function in helper class which consist connection as well
public NpgsqlCommand GetSqlQueryCommand(string query)
{
return PrepareCommand(CommandType.Text, query);
}
private NpgsqlCommand PrepareCommand(CommandType commandType, string commandText)
{
if (connection == null)
{
connection = new NpgsqlConnection(this.connectionString);
}
if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
{
connection.Open();
}
NpgsqlCommand command = new NpgsqlCommand(commandText, connection);
command.CommandType = commandType;
return command;
}
public DataSet LoadDataSet(NpgsqlCommand command, string[] tableNames = null)
{
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(command))
{
using (DataSet ds = new DataSet())
{
da.Fill(ds);
return ds;
}
}
}
Comments
Post a Comment