SqlDataAdapter.Fill() - 转换溢出 [英] SqlDataAdapter.Fill() - Conversion overflow

查看:39
本文介绍了SqlDataAdapter.Fill() - 转换溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我在用于十进制字段的 SqlDataAdapter.Fill() 用法之一中遇到转换溢出"异常.错误发生在 100 亿开始的值,但直到 10 亿才出现.代码如下:

I am encountering "Conversion overflow" exceptions on one of the SqlDataAdapter.Fill() usages for a decimal field. The error occurs for value beginning 10 billion, but not till 1 billion. Here is the code:

DataSet ds = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter();

adapter.SelectCommand = <my SQL Command instance>
adapter.Fill(ds);

我读过使用 SqlDataReader 作为替代,但我们需要明确设置数据类型和精度.我正在获取至少 70 列,我不想将所有列都设置为错误的一个十进制字段.

I have read using SqlDataReader as an alternate but we need to set the datatype and precision explicitly. There are at least 70 columns that I am fetching and I don't want to set all of them only for one decimal field in error.

有人可以建议替代方法吗?

Can anyone suggest alternate approaches?

谢谢.

推荐答案

虽然允许数据集填充"数据适配器,但我通常使用 DataTable 来代替查询时,我只期待一个结果集.话虽如此,我会预查询表,只是为了得到它的结构......像

Although dataset is allowed for "filling" a data adapter, I've typically done with a DataTable instead as when querying, I'm only expecting one result set. Having said that, I would pre-query the table, just to get its structure... something like

select whatever from yourTable(s) where 1=2

这将在您执行

DataTable myTable = new DataTable();
YourAdapter.Fill( myTable );

既然您有一个本地表,该表不会因内容大小而失败,因为不会返回任何记录,您现在可以明确转到有问题的那一列并根据需要设置其数据类型/大小信息...

Now that you have a local table that will not fail for content size because no records will have been returned, you can now explicitly go to that one column in question and set its data type / size information as you need...

myTable.Columns["NameOfProblemColumn"].WhateverDataType/Precision = Whatever you need...

现在,您的本地架构是合法的,问题列将以其精确度被识别.现在,使用适当的 where 子句而不是 1=2 放入适当的查询以实际返回数据...由于第一遍中没有实际行,因此您甚至不需要执行 myTable.Clear() 清除行...只需重新运行查询和 dataAdapter.Fill().

NOW, your local schema is legit and the problem column will have been identified with its precision. Now, put in your proper query with proper where clause and not the 1=2 to actually return data... Since no actual rows in the first pass, you don't even need to do a myTable.Clear() to clear the rows... Just re-run the query and dataAdapter.Fill().

我实际上并没有尝试过,因为我没有您的数据问题来模拟相同的问题,但是理论过程应该可以让您无需明确地浏览所有列...只是可能会造成问题的少数列.

I haven't actually tried as I don't have your data issues to simulate same problem, but the theoretical process should get you by without having to explicitly go through all columns... just the few that may pose the problem.

这篇关于SqlDataAdapter.Fill() - 转换溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆