简体   繁体   English

当不同表中的两列具有相同名称时,如何使用SqlDataReader读取数据?

[英]How to read data using SqlDataReader when two columns in different tables have same name?

I have two columns named 'Name' in two different tables. 我在两个不同的表中有两个名为“名称”的列。 One is in the Store table the other is in the Product table. 一个在商店表中,另一个在产品表中。 How do I differentiate which one is read? 我该如何区分阅读哪一个?

The data is taken from 4 different tables using left joins as shown in the query below. 数据使用左连接从4个不同的表中获取,如下查询所示。

var conn = new SqlConnection("serverinfo");
SqlCommand query = new SqlCommand("SELECT Store.StoreID, Store.Name, 
Product.Name, " +
" StockRequest.Quantity, StoreInventory.StockLevel from Store" +
" LEFT JOIN StoreInventory ON StoreInventory.StoreID = Store.StoreID" 
+ " LEFT JOIN Product ON StoreInventory.ProductID = 
Product.ProductID" + " LEFT JOIN StockRequest ON StockRequest.StoreID 
= Store.StoreID", conn);

SqlDataReader read;

        try {
            conn.Open();
            read = query.ExecuteReader();
            while (read.Read()) {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  read["StoreID"],
                                  read["Name"],
                                  read["Name"],
                                  read["Quantity"],
                                  read["StockLevel"]);

            }
            conn.Close();
            } catch (Exception e) {}

The table names are Store, Product, StockRequest, StoreInventory. 表名称是商店,产品,库存请求,商店库存。

Right now the data for both of the 'Name' columns are taken from the Store table. 现在,两个“名称”列的数据都来自“存储”表。 I am unable to take the 'Name' data from the Product table. 我无法从“产品”表中获取“名称”数据。

You could use alias to give a different name to the duplicate column. 您可以使用别名为重复的列命名。

SqlCommand query = new SqlCommand("SELECT Store.StoreID, 
"Store.Name as StoreName," + 
"Product.Name as ProductName," +
" StockRequest.Quantity, StoreInventory.StockLevel from Store" +
" LEFT JOIN StoreInventory ON StoreInventory.StoreID = Store.StoreID" 
+ " LEFT JOIN Product ON StoreInventory.ProductID = 
Product.ProductID" + " LEFT JOIN StockRequest ON StockRequest.StoreID 
= Store.StoreID", conn);

Then just use the right name when reading it, like: 然后在阅读时使用正确的名称即可,例如:

read["StoreID"],
read["StoreName"], read["ProductName"],
read["Quantity"], read["StockLevel"]

暂无
暂无

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

相关问题 如何在数据网格中显示来自联接表和表1的数据,并且两个具有相同名称的列 - how to display data in data grid that is from joined tables and table 1 and two have columns with the same name 如何使用SqlDataReader从没有列名的数据读取 - How to read from a data with no column name using SqlDataReader 如何从SqlDataReader同时读取两行值 - How to read two rows values at the same time from a SqlDataReader Linq 使用 .net 中的“联接”从具有相同名称的 2 个不同表的 2 列中获取数据? - Linq to get the data from 2 columns having same name of 2 different tables using "Joins" in .net? 使用SQLDataReader时,“在没有数据的情况下读取尝试无效” - “Invalid attempt to read when no data is present” when using SQLDataReader 如何比较实体框架中具有相同表名的两个模型之间的数据? - How to compare the data between two models that have the same tables name in Entity Framework? 如何在两列不同的表中的字符串中查找公用名? - How to find in a string a commun name in two columns, different tables? 使用城堡ActiveRecord,当两个类具有相同的名称但名称空间不同时,我得到了NHibernate DuplicateMappingException - Using Castle ActiveRecord, I get an NHibernate DuplicateMappingException when two classes have the same name but different namespaces NHibernate DuplicateMappingException 当两个类具有相同的名称但不同的命名空间时 - NHibernate DuplicateMappingException when two classes have the same name but different namespaces 如何在GridView中有两列具有相同字段但值不同 - How to have two columns with the same field but different values in a GridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM