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.
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.
SqlCommanddoesn't include any table name !SqlCommandand read the tables that containNameseparately ?