开发者

How do I get an DataTable from IDataReader?

开发者 https://www.devze.com 2023-04-05 01:25 出处:网络
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\";

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号