2017-02-26 104 views
0

我如何在內部連接查詢中正常選擇表/列?SQL內部連接和正常選擇

看看這個代碼SQL CODE HERE

這是我如何填充我DGV

SqlConnection con = new SqlConnection("Data Source=DESKTOP-5V9PS33\\SQLEXPRESS;Initial Catalog=Farmacia;Integrated Security=True"); 
      con.Open(); 
      SqlCommand sc = new SqlCommand("SELECT Category,Subcategory,Product,Supplier FROM Inventory Inner Join Category ON Category.ID = Inventory.CategoryID Inner Join Subcategory ON Subcategory.ID = Inventory.SubcategoryID Inner Join Product ON Product.ID = Inventory.ProductID Inner Join Supplier ON Supplier.ID = Inventory.SupplierID", con); 
      SqlDataReader reader; 
      reader = sc.ExecuteReader(); 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("ID", typeof(int)); 
      dt.Columns.Add("Product", typeof(string)); 
      dt.Columns.Add("Category", typeof(string)); 
      dt.Columns.Add("Subcategory", typeof(string)); 
      dt.Columns.Add("Supplier", typeof(string)); 
      dt.Load(reader); 

      for (int x = 0; x < dt.Rows.Count; x++) 
      { 
       string ID = dt.Rows[x].ItemArray[0].ToString(); 
       string Product = dt.Rows[x].ItemArray[1].ToString(); 
       string Category = dt.Rows[x].ItemArray[2].ToString(); 
       string Subcategory = dt.Rows[x].ItemArray[3].ToString(); 
       string Supplier = dt.Rows[x].ItemArray[4].ToString(); 
       string[] row = { ID,Product, Category, Subcategory, Supplier }; 
       dgvInventory.Rows.Add(row); 

      } 

我需要輸出的ID連同一個查詢的其他信息,這樣我就可以把它們放到一個數據表,然後使用它填充DGV

回答

0

Inventory.Id添加到您的select查詢中。

SELECT Inventory.Id, Category,Subcategory,Product,Supplier FROM Inventory ...

+0

感謝您的快速答覆!真的有幫助。 +代表哥哥 –

+0

@CopenhagenLlagas樂意幫忙! – SqlZim