复制到数据表时,对于 Int16 来说值太大或太小
问:
下面的代码:
var dtInstTotal = dtExternal.AsEnumerable()
.Union(dtEmployed.AsEnumerable())
.OrderBy(d => d.Field<string>("emp_name"));
dtInst = dtInstTotal.CopyToDataTable();//exception
抛出异常:
对于 Int16 来说值太大或太小。无法存储 <103930>在 emp_num 列中。预期类型为 Int16。 ---> System.OverflowException:值太大或太小 一个 Int16。
Q:
The following code:
var dtInstTotal = dtExternal.AsEnumerable()
.Union(dtEmployed.AsEnumerable())
.OrderBy(d => d.Field<string>("emp_name"));
dtInst = dtInstTotal.CopyToDataTable();//exception
throw an exception:
Value was either too large or too small for an Int16.Couldn't store
<103930> in emp_num Column. Expected type is Int16. --->
System.OverflowException: Value was either too large or too small for
an Int16.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑
dtExternal
具有emp_num
的short
类型,而dtEmployed
具有其他类型(int
、long
或可能只是ushort
) - 或者反之亦然。CopyToDataTable
仅使用包含它看到的第一行的第一个表中的类型,然后当它遇到来自某个表的具有相同名称的列的值时就会出现问题不同的表。来自文档:基本上:确保两个原始表具有相同的架构。
编辑:我们不知道填充两个原始
DataTable
的方法是什么样的 - 但您可能会发现,通过首先创建DataTable
,显式设置emp_num
,然后然后填充表格,就可以了。您甚至可以保留原来的方法,并使用正确的架构构建一个新的
DataTable
,然后调用I suspect that
dtExternal
has ashort
type foremp_num
, whereasdtEmployed
has some other type (int
,long
or maybe justushort
) - or maybe vice versa.CopyToDataTable
just uses the types from the first table containing the first row it sees, and then it's having problems when it comes across a value for a column with the same name from a different table. From the docs:Basically: make sure your two original tables have the same schema.
EDIT: We don't know what your methods to populate the two original
DataTable
s look like - but you may find that by creating theDataTable
first, explicitly setting the type ofemp_num
, and then filling the table, that will be okay.You could even leave your original methods alone, and build a new
DataTable
with the right schema, then call我认为 emp_num 的 dtInst 或 dtInstTotal 中的数据表结构是
int16
将其更改为int32
i think the datatable structure in dtInst or dtInstTotal for emp_num is
int16
change it toint32