简体   繁体   English

在同一条语句中多次从同一表中选择同一列

[英]Selecting the same column from the same table multiple times in the same statement

I am converting a VB6 app to C# with an SQL Server back end. 我正在使用SQL Server后端将VB6应用程序转换为C#。 The app includes a very general query editor that allows the user to write any select query and return the results visually in a grid control. 该应用程序包括一个非常通用的查询编辑器,该编辑器允许用户编写任何选择查询并在网格控件中直观地返回结果。 Some of the tables have several hundred columns (poor design, I know but I have no control over this). 有些表有几百列(我知道设计很差,但是我对此无能为力)。 A typical use case for an admin user would be to 管理员用户的典型用例是

select * from A_Table_With_Many_Columns

However, while they want to be able to view all the data, they are particularly interested in 2 columns and they want these to be displayed as the first 2 columns in the grid (instead of 67th and 99th for example) so instead they execute the following statement: 但是,尽管他们希望能够查看所有数据,但他们对2列特别感兴趣,并且希望将它们显示为网格中的前2列(例如,而不是第67和99列),因此他们执行了以下陈述:

select First_Interesting_Field, Second_Interesting_Field, * 
from A_Table_With_Many_Columns

Then they will go and modify the data in the grid. 然后他们将去修改网格中的数据。 However, when saving this data, it results in a concurrency violation ( DBConcurrencyException ). 但是,保存此数据时,将导致并发冲突( DBConcurrencyException )。 This worked fine with the connected RecordSets of VB6 but not so well in C#. 这与VB6的已连接RecordSets一起正常工作,但在C#中效果不佳。 I have tried a myriad of solutions to no avail. 我尝试了无数种解决方案。

  1. Does anyone know how to handle this exception in a generic way? 有谁知道如何以通用方式处理此异常? (Remember, the user can type ANY select statement or join etc. into the query editor) (请记住,用户可以在查询编辑器中键入ANY select语句或join等)

  2. Does anyone know how I might manipulate the columns returned such that I delete the 2 columns that appear further on in the list? 有谁知道我将如何处理返回的列,以便删除列表中进一步显示的2列? (My difficulty here is that if the column name in the database is EMail so I do select Email, * from Blah the 2 pertinent columns returned are EMail and ADO.NET or C# aliases the second EMail column from the * portion of the query as EMail1 so I am not able to detect the second column as a duplicate and remove it) (我的困难是,如果数据库中的列名是EMail那么我确实select Email, * from Blah中返回的2个相关列是EMail而ADO.NET或C#将查询的*部分中的第二个EMail列别名为EMail1因此我无法将第二列检测为重复并将其删除)

  3. Does anyone have an alternate solution I have not thought of? 有人有我没想到的替代解决方案吗?

Thank you very much 非常感谢你

Actually, you could rename all variables to something like email_userdefined by doing something like this: 实际上,您可以通过执行以下操作将所有变量重命名为email_userdefined:

SELECT First_Interesting_Field as First_Interesting_Field_userdefined, Second_Interesting_Field as Second_Interesting_Field_userdefined, * from A_Table_With_Many_Columns 选择First_Interesting_Field作为First_Interesting_Field_userdefined,Second_Interesting_Field作为Second_Interesting_Field_userdefined,*从A_Table_With_Many_Columns

Replace user_defined with whatever you want, like order number or anything else user acceptable 将user_defined替换为所需的内容,例如订单号或其他任何用户可接受的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM