2017-02-26 19 views
0

内部結合クエリで通常どのようにテーブル/列を選択できますか?SQL内部結合と通常選択

このコードを見SQL CODE HERE

これは私がその後、1つのデータテーブルにそれらを置くことができるので、私は一緒に1つのクエリ内の他の情報とを出力するIDを必要とする私は私の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); 

      } 

を移入する方法でありますDGVにそれを設定する

答えて

0

selectクエリにInventory.Idを追加します。

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

+0

お返事ありがとうございました!本当に助けます。 + rep brother –

+0

@CopenhagenLlagasお手伝いを! – SqlZim

関連する問題