Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an DataTable from IDataReader?

Tags:

c#

sql

idbcommand

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())
                    {
                        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?

like image 910
Guilherme Oliveira Avatar asked Oct 14 '25 03:10

Guilherme Oliveira


1 Answers

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);
like image 97
alloc_iNit Avatar answered Oct 16 '25 19:10

alloc_iNit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!