如何从 IDataReader 获取 DataTable?
我试图从 IDataReader
获取 DataTable
或 DataSet
,但失败了。代码如下:
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);
}
}
}
}
我使用 IDbConnection
和 IDbCommand
因为它适用于三个不同的数据库(方法 CreateConnection(provider, connectionString)
得到具体连接类型根据数据库而定)。 我的查询获取一个 ID(作为 Int64)和一个 DocNumber(作为 Decimal),但每次我尝试获取十进制值时,它都会抛出一个 OverflowException
并显示一条消息:“转换溢出”。这两个值对我来说都很重要,但我不知道如何获得这些值。
实际上,我并没有尝试将代码转换为 DataTable,我必须毫无例外地获取两者的值。
有帮助吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我没有通过执行进行检查,但它应该可以工作......
Though I haven't check by executing but it should work...