.net System.ArgumentException 列 <ColumnName> 不属于表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6015960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:35:22  来源:igfitidea点击:

System.ArgumentException Column <ColumnName> does not belong to table

.net.net-3.5

提问by S M Kamran

I am having a problem when accessing a DataColumn value in datatable with its name the System.ArgumentException Column <ColumnName> does not belong to tableoccurs 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.

我在访问数据表中的 DataColumn 值时遇到问题,其名称System.ArgumentException Column <ColumnName> does not belong to table出现在某些列中。但是,该列存在于数据库中,但大小写不同。我认为 DataTable 列名不区分大小写。任何人都知道为什么我会得到这个。在另一台机器上,此代码工作正常。我认为它与 SQL 合并没有任何关系,但在这种情况下,两个 SQL 服务器上的合并是相同的。

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"];

回答by S M Kamran

I've resolved it. Basically when a DataTable contains a simillar column name whose name is only different in Case i.e AwardTypeand awardTypeare 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.

我已经解决了。基本上,当 DataTable 包含一个相似的列名称时,其名称仅在 Case 中不同,即AwardTypeAwardType是相同的列名称,但仅在拼写大小写上有所不同。如果这样的冲突发生在 DataTable 内部,这使得对所有 DataTable 列的访问区分大小写。

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.

就我而言,开发人员应用了一些连接来获取数据表中的数据。然而,列数没有被过滤,不幸的是,在 2 个不同的表中,其中一个列名是相似的,但写的是不同的大小写。我没能注意到,因为数据表很大。最后我发现了这一点。因此,为列名之一设置别名对我有用。