简体   繁体   English

如何从IDataReader获取DataTable?

[英]How do I get an DataTable from IDataReader?

I'm trying to get a DataTable or DataSet from an IDataReader , but I'm failing. 我正在尝试从IDataReader获取DataTableDataSet ,但我失败了。 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). 我正在使用IDbConnectionIDbCommand因为它可以使用三个不同的数据库(方法CreateConnection(provider, connectionString)根据数据库获取特定类型的连接)。 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." 我的查询获取一个ID(作为Int64)和一个DocNumber(作为十进制),但每次我尝试获取十进制值时,它都会抛出一个OverflowException并显示一条消息:“转换溢出”。 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. 实际上,代码我不是要尝试转换为DataTable ,我必须毫无例外地获得两者的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM