How do I get an DataTable from IDataReader?
I'm trying to get a DataTable
or DataSet
from an IDataReader
, but I'm failing. Here's the code:
string sql = @"SELECT ID, DOCNUMBER FROM TBDOCUMENT";
using (IDbConnection conn = CreateConnection(provider, connectionString))
{
conn.Open();
using (IDbCommand command = conn.CreateCommand())
{
开发者_Python百科 command.CommandText = sql;
IDataReader reader = command.ExecuteReader();
using (reader)
{
while (reader.Read())
{
long key = reader.GetInt64(0);
decimal value = reader.GetDecimal(1);
}
}
}
}
I'm using IDbConnection
and IDbCommand
because it will works with three different databases (the method CreateConnection(provider, connectionString)
gets the specific type of connection according to the database).
My query gets an ID (as Int64) and a DocNumber (as Decimal), but every time I try to get the decimal value, it throws an OverflowException
with a message: "Conversion overflows." Both of values are important to me, but I don't know how do I get these values.
Actually, the code I'm not trying to convert to a DataTable
, I have to get the value of the two without exception.
Some help?
Though I haven't check by executing but it should work...
// Load the data into the existing DataSet.
DataTableReader reader = GetReader();
dataSet.Load(reader, LoadOption.OverwriteChanges,
customerTable, productTable);
// Load the data into the DataTable.
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
精彩评论