简体   繁体   中英

How to insert a row from one table to another table using Linq to SQL

I am having to two tables Data_Cust_Log and Data_Cust .The structure of both the table is same.When a customer is authorized the row of data for that customer from Data_Cust_Log needs to be copied to Data_Cust .Can anybody let me know if this can be done using Linq to SQL . Any suggestions are welcome. Thanks.

If the objects are of the same type:

 using (DataClasses1DataContext context = new DataClasses1DataContext())
 {
    var data = context.Data_Cust_Log.Where(x => x.CustomerID == 12) Select(x => x).FirstOrDefault();
    context.Data_Cust.InsertOnSubmit(data);
    context.SubmitChanges();
 }

If they are not of the same type:

using (DataClasses1DataContext context = new DataClasses1DataContext())
{
    var data = context.Data_Cust_Log.Where(x => x.CustomerID == 12) Select(x => x).FirstOrDefault();

    Data_Cust_Object = new Data_Cust_Object {CustomerID = data.CustomerID, Price = data.Price}; //and so on 

    context.Data_Cust.InsertOnSubmit(Data_Cust_Object);
    context.SubmitChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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