System.ArgumentException 列不属于表
我在访问数据表中名称为 System.ArgumentException Column
。然而,该列存在于数据库中,但大小写不同。我认为 DataTable 列名称不区分大小写。任何人都知道我为什么会得到这个。在另一台机器上,此代码运行良好。我不认为它与 SQL 联盟有任何关系,但在这种情况下,两个 SQL 服务器上的联盟是相同的。
vpg_awardtype = row["vpg_awardtype"];
vpg_eventcount = row["vpg_eventcount"];
如果我将其更改为以下内容,那么它就可以工作:
vpg_awardtype = row["Vpg_AwardType"];
vpg_eventcount = row["Vpg_EventCount"];
I am having a problem when accessing a DataColumn value in datatable with its name the System.ArgumentException Column <ColumnName> does not belong to table
occurs for some of the columns. However the column exists in the database but with a different case. I thought DataTable column names are case insensitive. Any 1 got any idea why I am getting this. On a different machine this code is working fine. I don't think it has any thing to do with SQL Coallation but in this case the coallation are same on both SQL servers.
vpg_awardtype = row["vpg_awardtype"];
vpg_eventcount = row["vpg_eventcount"];
If i change it to the following then it's working:
vpg_awardtype = row["Vpg_AwardType"];
vpg_eventcount = row["Vpg_EventCount"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了。基本上,当 DataTable 包含类似的列名称时,其名称仅在大小写上不同,即 AwardType 和 awardType 是相同的列名称,但仅在拼写大小写上不同。如果此类冲突发生在 DataTable 内部,这会使对所有 DataTable 列的访问区分大小写。
就我而言,开发人员应用了一些联接来获取数据表中的数据。然而,列数没有被过滤,不幸的是,在两个不同的表中,其中一个列名称相似,但大小写不同。我无法注意到这一点,因为数据表很大。最后我通过艰难的方式发现了这一点。因此,别名列名之一对我有用。
I've resolved it. Basically when a DataTable contains a simillar column name whose name is only different in Case i.e AwardType and awardType are same column names but only different in spelling case. If such a collision occur inside a DataTable This makes the access to all DataTable columns as case sensitive.
In my case there was some joins applied by a developer to get the data in the datatable. However the number of columns were not filtered and unfortunately in 2 different tables one of the column name was simillar but written with different case. I wasn't able to notice that because the datatable was huge. Finally I found this out the hard way. Hence aliasing one of the column name worked for me.