简体   繁体   English

C#SQL是否可以从联接表插入到datagridview中

[英]C# SQL Is it possible to insert from a joined table into datagridview

I have four tables in my SQL database 我的SQL数据库中有四个表

数据关系图

In C# I have made a datagridview which shows the DeliveryDate, Quantity, Description and Price from the Order, Linkedtable and Stock table being joined in a select statement. 在C#中,我创建了一个datagridview,该视图显示了select语句中连接的Order,Linkedtable和Stock表中的DeliveryDate,数量,描述和价格。

Here is the code used to join three tables into one datagridview 这是用于将三个表连接到一个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();

This is how the datagridview looks 这就是datagridview的样子 join语句在datagridview中的外观

I would like to insert DeliveryDate, Quantity, Description and price from four textboxes into datagridview but i'm not sure how to do an INSERT statement with the tables joined like i have with the SELECT statement, is there a way to do this? 我想将四个文本框中的DeliveryDate,Quantity,Description和Price插入到datagridview中,但是我不确定如何使用与SELECT语句相同的连接表来执行INSERT语句,有没有办法做到这一点?

Since data will reside in different tables, there is no way to insert them at once and therefore, you can't execute it with one single command. 由于数据将驻留在不同的表中,因此无法立即插入它们,因此,您无法使用一个命令来执行它。 However, you can create a store procedure to insert data into different tables separately, or you can insert them with two different INSERT commands in one batch. 但是,您可以创建一个存储过程以将数据分别插入到不同的表中,或者可以用两个不同的INSERT命令将它们批量插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM