Use inner join with C# SQL Server(Retrieving data from multiple table using SQL query and inner join in C# ADO.NET)

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 Server 中尝试这个查询时成功了,但我不知道在 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

我建议使用parameters以避免Sql注入

cmd.SelectCommand.Parameters.Add("@SupplierId", SqlDbType.Int).Value = supplier_id ;

您还可以使用 ORM 将查询映射到对象,例如 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);