簡體   English   中英

SQL內部連接和普通選擇

[英]SQL Inner join and normal select

我通常如何在內部聯接查詢中選擇表/列?

看看這段代碼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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM