![](/img/trans.png)
[英]Problem Retrieving Data from SQL Server using C# and ADO.Net window form
[英]Use inner join with C# SQL Server (Retrieving data from multiple table using SQL query and inner join in C# ADO.NET)
我正在嘗試將兩個不同表中的數據檢索到一個數據表中,並分配到 C# WinForms 中的文本框中。 雖然我在 SQL 服務器中嘗試此查詢時成功了,但我不知道在 WinForms 上實現它。 這是我的代碼中嘗試過的:
string supplier_id = dataGridView1.CurrentRow.Cells[1].Value.ToString();
SqlDataAdapter cmd = new SqlDataAdapter("select New_Supplier.Supplier_Name, New_Supplier.Address, New_Supplier.City, New_Supplier.Contact_No ,Purchase_Entry.Balance from New_Supplier,Purchase_Entry where New_Supplier.'" + supplier_id + "' = Purchase_Entry.'" + supplier_id + "' ", con);
DataTable dt = new DataTable(); cmd.Fill(dt);
Purchase_Entry.Instance.txtsuppliername.Text = dt.Rows[0][0].ToString();
Purchase_Entry.Instance.txtaddress.Text = dt.Rows[0][1].ToString();
Purchase_Entry.Instance.txtcity.Text = dt.Rows[0][2].ToString();
Purchase_Entry.Instance.txtcontactno.Text = dt.Rows[0][3].ToString();
Purchase_Entry.Instance.lblbalance.Text = dt.Rows[0][4].ToString();
也試過這個:
string supplier_id = dataGridView1.CurrentRow.Cells[1].Value.ToString(); MessageBox.Show(supplier_id);
SqlDataAdapter cmd = new SqlDataAdapter($"select New_Supplier.Supplier_Name, New_Supplier.Address, New_Supplier.City, New_Supplier.Contact_No ,Purchase_Entry.Balance from New_Supplier,Purchase_Entry where New_Supplier.{supplier_id} = Purchase_Entry.{supplier_id} ", con);
DataTable dt = new DataTable(); cmd.Fill(dt);
Purchase_Entry.Instance.txtsuppliername.Text = dt.Rows[0][0].ToString();
Purchase_Entry.Instance.txtaddress.Text = dt.Rows[0][1].ToString();
Purchase_Entry.Instance.txtcity.Text = dt.Rows[0][2].ToString();
Purchase_Entry.Instance.txtcontactno.Text = dt.Rows[0][3].ToString();
Purchase_Entry.Instance.lblbalance.Text = dt.Rows[0][4].ToString();
但是在獲取數據時連接查詢時出錯在 SQL 服務器中嘗試過並且它有效
select New_Supplier.Supplier_Name, New_Supplier.Address, New_Supplier.City, New_Supplier.Contact_No ,Purchase_Entry.Balance from New_Supplier,Purchase_Entry where New_Supplier.supplier_id = Purchase_Entry.supplier_id
它讓我回到了我想要的東西:
查詢可能像
select supplier.Supplier_Name, supplier.Address, supplier.City,
supplier.Contact_No, purchase.Balance
from New_Supplier supplier
join Purchase_Entry purchase on supplier.supplier_id = purchase.supplier_id
where purchase.supplier_id = @SupplierId
我建議使用參數以避免 Sql 注入
cmd.SelectCommand.Parameters.Add("@SupplierId", SqlDbType.Int).Value = supplier_id ;
您也可以使用 ORM 到 map 查詢到 object,就像dapper
因為用數組映射數據表有點大。 如果您更改 select 的查詢和順序,可能會顯示錯誤的數據
我覺得你應該這樣寫
SqlDataAdapter cmd =new SqlDataAdapter(
"select
New_Supplier.Supplier_Name,
New_Supplier.Address,
New_Supplier.City,
New_Supplier.Contact_No,
Purchase_Entry.Balance
from New_Supplier,Purchase_Entry
where New_Supplier.supplier_id = Purchase_Entry.supplier_id
and New_Supplier.supplier_id='"+supplier_id +"'
", con);
要么
SqlDataAdapter cmd =new SqlDataAdapter(
"select
New_Supplier.Supplier_Name,
New_Supplier.Address,
New_Supplier.City,
New_Supplier.Contact_No,
Purchase_Entry.Balance
from New_Supplier
inner join Purchase_Entry on New_Supplier.supplier_id =
Purchase_Entry.supplier_id
where New_Supplier.supplier_id='"+supplier_id +"'
", con);
但最好使用 SQL 參數而不是連接字符串,因為 SQL 注入如下所示:
SqlCommand cmd = new SqlCommand(
@"select
New_Supplier.Supplier_Name,
New_Supplier.Address,
New_Supplier.City,
New_Supplier.Contact_No,
Purchase_Entry.Balance
from New_Supplier
inner join Purchase_Entry on New_Supplier.supplier_id =
Purchase_Entry.supplier_id
where New_Supplier.supplier_id= @supplier_id", con);
cmd.Parameters.Add("@supplier_id",SqlDbType.Int).Value = supplier_id;
cmd.CommandType = System.Data.CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.