sqlDatareader

  • Thread starter Thread starter Analizer1
  • Start date Start date
A

Analizer1

Im looking for example of how to get data into a table
without using Table.load(reader)
'

using the read() in a loop
SqlDataReader reader = Cmd.ExecuteReader();

while (reader.read())

{

//do some work ehre

}
 
Analizer1 said:
Im looking for example of how to get data into a table
without using Table.load(reader)
'

using the read() in a loop
SqlDataReader reader = Cmd.ExecuteReader();

while (reader.read())

{

//do some work ehre

}

You could use the method LoadDataRow to load each row as you read them from
the datareader:

while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
theTable.LoadDataRow(values, true);
}
 
i tried that. I think i was using it incorrectly....
i'll let You know ...
Thanks


Alberto Poblacion said:
Analizer1 said:
Im looking for example of how to get data into a table
without using Table.load(reader)
'

using the read() in a loop
SqlDataReader reader = Cmd.ExecuteReader();

while (reader.read())

{

//do some work ehre

}

You could use the method LoadDataRow to load each row as you read them
from the datareader:

while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
theTable.LoadDataRow(values, true);
}
 
well Figured it out..the below is just a working model ...by no means
complete
Thanks for the help....very much appreciated.....
The Below is a working test of multiple rowsets from a single call to a
Stored Proc


namespace testApp
{
class Program
{
static void Main(string[] args)
{
dvSqlConnection SqlConn1 = new
dvSqlConnection("SERVER=support35;database=OfficeAlly;User
Id=oaServiceadmin;Password=admin;CONNECTION TIMEOUT=0");
Fill3TablesAtOnce(SqlConn1);


}
public static void Fill3TablesAtOnce(dvSqlConnection SqlConn1)
{
DataTable dtCustomers=null;
DataTable dtOrders;
DataTable dtTransActions;
SqlCommand Cmd = new SqlCommand();
Cmd.Parameters.Clear();
Cmd.CommandText = "SomeProc";
Cmd.Connection = SqlConn1.Conn;
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandTimeout = 180;
Cmd.Parameters.Add("ReturnCode", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue;
Cmd.Parameters.Add("CustomerId", SqlDbType.Int).Value =
10494415;
SqlConn1.Open()
SqlDataReader reader = Cmd.ExecuteReader();
//get schema table for 1st result set
DataTable schema = reader.GetSchemaTable();
//create table from schema
dtCustomers = CreateTable(schema);
//create array of total columns (fields)
object[] columnvalues = new object[reader.FieldCount];
//read 1st resultset
while (reader.Read())
{

reader.GetValues(columnvalues);
dtCustomer.LoadDataRow(columnvalues,true);
}
//2nd result set
reader.NextResult();
schema = reader.GetSchemaTable();
dtOrders = CreateTable(schema);
columnvalues = new object[reader.FieldCount];
while (reader.Read())
{

reader.GetValues(columnvalues);
dtOrders.LoadDataRow(columnvalues, true);
}
//3rd result set
reader.NextResult();
schema = reader.GetSchemaTable();
dtTransActions = CreateTable(schema);
columnvalues = new object[reader.FieldCount];
while (reader.Read())
{

reader.GetValues(columnvalues);
dtTransActionss.LoadDataRow(columnvalues, true);
}
Console.WriteLine(dtCustomers.Rows.Count.ToString());
Console.WriteLine(dtOrders.Rows.Count.ToString());
Console.WriteLine(dtTransActions.Rows.Count.ToString());
}

public static DataTable CreateTable(DataTable Schema)
{
DataTable NewTable = new DataTable();
DataColumn Col = new DataColumn();

for (int x=0;x<Schema.Rows.Count;x++)
{
DataRow row = Schema.Rows[x];
string ColumnName = (string)row["ColumnName"];
Col = new DataColumn(ColumnName, (Type)row["DataType"]);

NewTable.Columns.Add(Col);
}
return NewTable;
}
}

}
 
Back
Top