简体   繁体   English

在数据集中重复列名

[英]repeating column names in dataset

I have in database 2 identical tables - let's say ARTICLE and ARTICLE_BACKUP. 我在数据库中有2个相同的表-假设是ARTICLE和ARTICLE_BACKUP。 They have different records but all the columns are the same. 它们具有不同的记录,但所有列均相同。

I can join them and read into dataset in C# 我可以加入他们并读入C#中的数据集

SELECT * from ARTICLE A INNER JOIN ARTICLE_BACKUP A2 ON A.ID=A2.ID

In SQL when working on joined tables you use table name and column name like 在SQL中,在处理联接表时,您可以使用表名和列名,例如

ARTICLE_BACKUP.NAME

so you will never mix columns with identical names from different tables. 因此,您永远不会将来自不同表的具有相同名称的列混合在一起。

When you read it to dataset, the table names are gone however. 当您将其读取到数据集时,表名消失了。 ARTICLE.NAME will be called just NAME and ARTICLE_BACKUP.NAME will be in most cases called NAME1 - "1" is added to the name, 在大多数情况下,ARTICLE.NAME将仅被命名为NAME,而ARTICLE_BACKUP.NAME将被命名为NAME1-名称中添加了“ 1”,

But what if NAME1 column already existed in sql query like: 但是,如果NAME1列已存在于SQL查询中,例如:

SELECT A.NAME,A.NAME1,A.NAME11, B.NAME,B.NAME1,B.NAME11, C.NAME,C.NAME1,C.NAME11, FROM A INNER JOIN B ON A.ID=B.ID INNER JOIN C on A.ID=C.ID 从A.ID = B.ID上的内连接B选择A.NAME,A.NAME1,A.NAME11,B.NAME,B.NAME1,B.NAME11,C.NAME,C.NAME1,C.NAME11 A.ID = C.ID上的INNER JOIN C

what will be the names of columns in dataset for the columns from B and C tables ? B和C表中的列的数据集中的列名称是什么?

I can check it but could anybody say the rule? 我可以检查一下,但是有人可以说规则吗?

Is there any 100% working way when having a column in sql query to know its name in dataset? 在sql查询中有一列以了解其在数据集中的名称时,是否有100%的工作方式?

"Is there any 100% working way when having a column in sql query to know its name in dataset?" “在sql查询中有一列以了解其在数据集中的名称时,是否有100%的工作方式?”

Yes, don't use select * . 是的,不要使用select * Manually type out the columns you want and you can alias them. 手动输入所需的列,然后可以对其进行别名。

Example

SELECT A.Column1 as AColumn1, A2.Column1 as A2Column1 from ARTICLE A INNER JOIN ARTICLE_BACKUP A2 ON A.ID=A2.ID

Then in your C# code you would access the columns via their aliased names. 然后,在您的C#代码中,您将通过它们的别名访问这些列。

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

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