在C#的SqlConnection同名的2 SQL列名之间的区别 [英] Differentiating between 2 SQL column names with the same name in a C# SqlConnection

查看:181
本文介绍了在C#的SqlConnection同名的2 SQL列名之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入了相同的2个表使用不同的别名,在SQL查询两次,以使我能够选择2个(可能但不总是)不同的地址的ID,然后链接到地址表中。

I have joined the same 2 tables twice using different aliases in a SQL query to enable me to select 2 (potentially but not always) different address ids which then link in to the address table.

SELECT C.court_id, C.court_name, CA.court_address, CA2.court_address...
FROM court C " +
JOIN court_addr CA ON C.court_addr_id = CA.court_addr_id " +
JOIN court_addr CA2 ON C.court_postal_addr_id = CA2.court_addr_id " + ...

现在试图输出,当使用ASP.NET C#我不确定这个查询的结果如何指定这两种地址的Response.Write的。投入列名称前面的别名(如下面的第4个字符串值)不工作,并提出了一个错误。在那里,尽管他们俩在C#中的两个地址之间的区别在数据库具有相同的列名的一种方式?

Now when trying to output the results of this query using ASP.NET C# I'm unsure how to specify which of the two addresses to Response.Write. Putting the alias in front of the column name (as in the 4th string value below) doesn't work and brings up an error. Is there a way of differentiating between the two addresses in C# despite them both having the same column name in the database?

while (myDataReader.Read())
{
    string court_id = myDataReader["court_id"].ToString();
    string court_name = myDataReader["court_name"].ToString();
    string court_address = myDataReader["court_address"].ToString();
    string court_postal_address = myDataReader["CA2.court_address"].ToString(); 
    etc.....

感谢您长久提前

推荐答案

您应该使用别名在SQL区分它们,那么你就可以返回正确的值:

You should use an alias in your sql to distinguish them, then you will be able to return the correct value:

SELECT C.court_id, 
  C.court_name, 
  CA.court_address as CACourtAddress, 
  CA2.court_address as CA2CourtAddress
FROM court C " +
JOIN court_addr CA ON C.court_addr_id = CA.court_addr_id " +
JOIN court_addr CA2 ON C.court_postal_addr_id = CA2.court_addr_id " + ...

这篇关于在C#的SqlConnection同名的2 SQL列名之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆