none
About the Insert, Update, and Delete operations in LINQ to SQL RRS feed

  • Question

  • Recently, I am dealing with the Insert, Update, and Delete operations in LINQ:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.Entity;
    using System.Data.Entity.Validation;
    
    namespace DefenderHyperGUI
    {
       public partial class DisplayPanelLog : Form
       {
          public DisplayPanelLog()
          {
             InitializeComponent();
          }
    
          private DefenderHyper.Defender_HyperEntities dbcontext =
          new DefenderHyper.Defender_HyperEntities();
    
          private void DisplayPanelLog_Load(object sender, EventArgs e)
          {
             dbcontext.PanelLogs
             .OrderBy(panelLog => panelLog.RecordID)
             .ThenBy(panelLog => panelLog.TimeStamp)
             .Load();
    
             panelLogBindingSource.DataSource = dbcontext.PanelLogs.Local;
          }
    
          private void panelLogBindingNavigatorSaveItem_Click(object sender, EventArgs e)
          {
             Validate(); // validate the input fields
             panelLogBindingSource.EndEdit(); // complete current edit, if any
    
             try
             {
                dbcontext.SaveChanges(); // write changes to database file
                MessageBox.Show("Saved");
             } // end try
             catch (DbEntityValidationException)
             {
                MessageBox.Show("RecordID and TimeStamp must contain values",
                   "Entity Validation Exception");
             } // end catch
          }
    
          private void button1_Click(object sender, EventArgs e)
          {
             var cust =
                (from c in dbcontext.PanelLogs
                 where c.RecordID == 1
                select c).First();
    
             cust.AccountID = 2;
    
             dbcontext.SaveChanges();
             MessageBox.Show("Saved");
          }
       }
    }
    

    I successfully work the change operation by clicking button 1. But anyone knows how can we add and delete the SQL records?

    I attached the link from MSDN which looks useful: https://msdn.microsoft.com/en-us/library/bb386931(v=vs.110).aspx

    Thanks

    Roy

    • Moved by Kristin Xie Monday, October 12, 2015 8:35 AM move to better forum
    Sunday, October 11, 2015 2:45 PM

Answers

  • You can see crud operations working in this sample of mine:

    http://social.technet.microsoft.com/wiki/contents/articles/31915.wpf-mvvm-step-by-step-1.aspx

    To insert:

    db.Customers.Add(entity);
    db.SaveChanges();

    Where db is dbcontext, Customers is your collection/table and entity is the new one.

    To delete you use Remove:

    db.Customers.Remove(entity);
    db.SaveChanges();


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    • Proposed as answer by KareninstructorMVP Sunday, October 11, 2015 5:51 PM
    • Marked as answer by e-mzRoy Tuesday, October 13, 2015 3:03 PM
    Sunday, October 11, 2015 3:08 PM

All replies