简体   繁体   English

如何使用 ASP.NET MVC 和 SQL 服务器将表字段值从一个表传递或复制到另一个表

[英]How to pass or copy table field value from one table to another table using ASP.NET MVC and SQL Server

I have two tables named Order and Invoice_Line_Items.我有两个名为 Order 和 Invoice_Line_Items 的表。 I have a column called "invoice_number" in both tables "invoice_number" and Invoice_Line_Items.我在两个表“invoice_number”和 Invoice_Line_Items 中都有一个名为“invoice_number”的列。 But "invoice_number" column in Invoice_Line_Items table is empty.但是 Invoice_Line_Items 表中的“invoice_number”列是空的。 I need to copy the data present in the "invoice_number" column from the Order table to "invoice_number" column in Invoice_Line_Items table.我需要将 Order 表中“invoice_number”列中的数据复制到 Invoice_Line_Items 表中的“invoice_number”列。

This is an example of what I want to achieve这是我想要实现的一个例子预期结果

Looks like the relationship between these 2 tables are Order_id ?看起来这两个表之间的关系是Order_id This is just the example:这只是一个例子:

var query = from it in db.Invoice_Line_Items
        join order in db.Order
        on it.Order_id equals order.Order_id  into lg
        from order in lg.DefaultIfEmpty()
        select new Invoice_Line_Items
        {
           Order_id = it.Order_id,
           Invoice_number = order == null? null: order.Invoice_number,
           item = it.item,
           quantity = it.quantity
        };

I managed to solve the issue and here is the code below我设法解决了这个问题,这是下面的代码

  1. changed the table name from order to ordering将表名从 order 更改为 ordering
  2. the invoice_number from Invoice_Line_Items made it retrieve the maximum number from ordering table order_id Invoice_Line_Items 中的 invoice_number 使其从订购表 order_id 中检索最大数量

     public ActionResult SaveOrder(Order[] requesting) { string result = "Error; Order Is Not Complete."; using (mymodel dbb = new mymodel()) { var customerId = Guid;NewGuid(). Ordering ord = new Ordering(); ord.CustomerId = customerId. ord;date_order_placed = DateTime.Now. dbb;Orderings.Add(ord). if (dbb.SaveChanges() > 0) { string order_id = dbb.Orderings;Max(o => o;invoice_number). foreach(var item in requesting) { Invoice_Line_Items in_l_i = new Invoice_Line_Items(); in_l_i.invoice_number = order_id. in_l_i;department = item.depart. in_l_i;service = item.Service. in_l_i;gender = item.Gender. in_l_i;item = item.Item. in_l_i;quantity = item.Quantity. in_l_i;price = item.price. dbb;Invoice_Line_Items.Add(in_l_i); } if (dbb,SaveChanges() > 0) { result = "Success. Order Is Complete;"; } } } return Json(result, JsonRequestBehavior.AllowGet); }

暂无
暂无

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

相关问题 如何使用ASP.NET MVC将数据从一个数据库表移动到同一数据库的另一表? - How to move data from one database table to another table of same database using ASP.NET MVC? SQL Server 2008,asp.net MVC 3 C#当该值为null时,将数据从一个表转移到另一个表。 。 另一个表中的数据开始 - SQL Server 2008 , asp.net MVC 3 C# Getting Data from one table to another when the value is null . . the data in the other table kicks in 如何在Asp.net MVC C#中使用Linq从一个表中选择具有最大计数值的多个表中的记录# - how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C# 使用ASP.NET MVC从记事本文件中读取数据并存储到SQL Server中的表中 - Read Data from Notepad File and Store into a Table in SQL Server using ASP.NET MVC 使用Enterprise Library从ASP.NET传递(数据表)到SQL Server - Pass (Data Table) to SQL Server From ASP.NET using Enterprise Library 如何从 ASP.NET MVC 中的表中获取每个值? - How to get each value from table in ASP.NET MVC? ASP.NET MVC将一个表复制到另一个表后从其中删除行 - ASP.NET MVC Deleting row from one table after copying it to another table 如何从 ASP.NET MVC 在 SQL 服务器的表中插入一些特殊列? - How to insert in some special columns in a table in SQL Server from ASP.NET MVC? ASP.NET MVC:一个Excel工作表,使用ASP.NET MVC将此数据插入到SQL Server表中 - Asp net mvc : one Excel sheet with insert this data into SQL Server table using Asp net mvc 我想使用asp.net mvc 5将一个表中的ID保存到另一个表中 - I want to save an ID from a table to another table using asp.net mvc 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM