【问题标题】:C# SQL Is it possible to insert from a joined table into datagridviewC# SQL 是否可以从连接表中插入 datagridview
【发布时间】:2024-01-10 08:49:01
【问题描述】:

我的 SQL 数据库中有四个表

在 C# 中,我创建了一个 datagridview,它显示了在 select 语句中连接的 Order、Linkedtable 和 Stock 表中的 DeliveryDate、Quantity、Description 和 Price。

这里是将三个表连接成一个datagridview的代码

// this code for the load button of the datagridview
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT O.DeliveryDate, L.Quantity, S.Description, S.Price FROM [Order] O JOIN Linkedtable L ON O.OrderID = L.OrderID JOIN Stock S ON S.StockID = L.StockID ORDER BY O.DeliveryDate ", con);
        DataTable DATA = new DataTable();
        sda.Fill(DATA);
        dataGridView1.DataSource = DATA;
        con.Close();

这就是 datagridview 的样子

我想将 DeliveryDate、Quantity、Description 和 price 从四个文本框插入到 datagridview 中,但我不知道如何像使用 SELECT 语句那样连接表来执行 INSERT 语句,有没有办法做这个?

【问题讨论】:

    标签: c# sql-server datagridview


    【解决方案1】:

    由于数据将驻留在不同的表中,因此无法一次插入它们,因此您无法使用单个命令执行它。但是,您可以创建一个存储过程来分别将数据插入到不同的表中,或者您可以使用两个不同的 INSERT 命令在一个批次中插入它们。

    【讨论】: