我不能繼續我的下一個任務,我的問題是我不知道如何將我的DGV的返回值與表匹配。UPDATE INNER JOIN WHERE條件
這是我如何加載我的組合框與值/顯示部件:
SqlConnection con = new SqlConnection("Data Source=DESKTOP-5V9PS33\\SQLEXPRESS;Initial Catalog=Farmacia;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("SELECT ID,ProductName FROM Product", con);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("ProductName", typeof(string));
dt.Load(reader);
cmbProductName.ValueMember = "ID";
cmbProductName.DisplayMember = "ProductName";
cmbProductName.DataSource = dt;
cmbProductName.Text = "";
這是我從我的數據庫增值,我DGV:
sc.Parameters.AddWithValue("@SupplierID", cmbSupplier.SelectedValue.ToString());
sc.Parameters.AddWithValue("@ProductNameID", cmbProductName.SelectedValue.ToString());
sc.Parameters.AddWithValue("@Quantity",txtQuantity.Text);
sc.Parameters.AddWithValue("@UnitPrice", txtUnitPrice.Text);
sc.Parameters.AddWithValue("@Markup", txtMarkup.Text);
sc.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);
sc.ExecuteNonQuery();
這裏是我的代碼爲「接收訂單」 UPDATE:
SqlConnection con = new SqlConnection("Data Source=DESKTOP-5V9PS33\\SQLEXPRESS;Initial Catalog=Farmacia;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("DELETE From PurchaseOrder WHERE ProductNameID = '"+dgvCategory.SelectedCells[1].Value.ToString()+"'", con);
sc.ExecuteNonQuery();
MessageBox.Show("Received");
填充DGV加載/刷新:
private void frmPurchaseOrder_Load_1(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-5V9PS33\\SQLEXPRESS;Initial Catalog=Farmacia;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("SELECT Supplier,ProductName,PurchaseOrder.Quantity,PurchaseOrder.UnitPrice,PurchaseOrder.Markup,PurchaseOrder.SellingPrice FROM PurchaseOrder Inner Join Supplier ON Supplier.ID = PurchaseOrder.SupplierID Inner Join Product ON Product.ID = PurchaseOrder.ProductNameID", con);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("Supplier", typeof(string));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("UnitPrice", typeof(float));
dt.Columns.Add("Markup", typeof(float));
dt.Columns.Add("SellingPrice", typeof(float));
dt.Load(reader);
for (int x = 0; x < dt.Rows.Count; x++)
{
string Supplier = dt.Rows[x].ItemArray[0].ToString();
string ProductName = dt.Rows[x].ItemArray[1].ToString();
string Quantity = dt.Rows[x].ItemArray[2].ToString();
string UnitPrice = dt.Rows[x].ItemArray[3].ToString();
string Markup = dt.Rows[x].ItemArray[4].ToString();
string SellingPrice = dt.Rows[x].ItemArray[5].ToString();
string[] row = { Supplier, ProductName, Quantity, UnitPrice, Markup, SellingPrice };
dgvCategory.Rows.Add(row);
}
con.Close();
}
catch (SystemException ex)
{
MessageBox.Show(ex.ToString());
}
}
我的DGV將只返回字符串或「產品」的名稱。
這裏是我的FullProduct表 Table HERE
窗口形式設計Form
如果我理解正確,你的問題是你不知道如何獲得ProductNameID?填充DGV的代碼是什麼? –
嘿@GiorgosAltanis檢查我的新編輯。 –