20150222-linq to SQL Insert, update, and delete

Source: Internet
Author: User

Note that when you use LINQ to SQL, you must have a primary key in the table to work, or the system will not be able to make modifications to the data

Insert new Data and add a new control to it based on the previous piece of the article instance:

Number TextBox (Name): Sno

Name textbox (name): sname

Gender textbox (Name): Ssex

Birthday DateTimePicker (Name): Bir

Class TextBox (Name): Sclass

and add buttons

Here is the code in the Add button:

Private voidTianjia_click (Objectsender, EventArgs e) {            stringSno = Bianhao. Text.trim ();//number textbox            stringsname = Mingzi. Text.trim ();//name textbox            stringSsex = Xingbie. Text.trim ();//sex textboxDateTime bir = Shengri. Value;//Birthday            stringSclass = Banji. Text.trim ();//class TextBox            if(Sno! =""|| Sname! =""|| Ssex! =""|| Sclass! ="") {dataclasses1datacontext DC=NewDataclasses1datacontext (); //initializes the class of this table based on the table name of the table to which you want to add informationStudent stu =NewStudent {Sno=Sno, Sname=sname, Ssex=Ssex, Sbirthday=Bir, Class=Sclass,}; //The following two lines of code are important code to modify the database, can not be omitted, otherwise you will not be able to modify the datadc. Student.insertonsubmit (Stu);//modifying in-memory datadc. SubmitChanges ();//writes the modified data to the database. MessageBox.Show ("Add success! "); }            Else{MessageBox.Show ("please fill in the complete information"); }        }

Attempt to add new data (note that when using LINQ to SQL, a primary key must be available in the table to be effective or the system will not be able to modify the data)

The results of the query again:

Update (Modify) data :

First modify the properties of the GridView, to facilitate our operation, the behavior of the value of SelectionMode to Fullrowselect, select a row

Then add a new event in the GridView (double-click on the right side of DoubleClick):

To add a new control to a form:
Number TextBox (Name): Xiugai_bianhao; fixed number, change its properties--behavior-enabled to False

Name textbox (name): Xiugai_mingzi

Gender textbox (Name): Xiugai_xingbie

Birthday DateTimePicker (Name): Xiugai_shengri

Class TextBox (Name): Xiugai_banji

and Modify Buttons

Then write the following code in the newly created double-click event:

Private voidDatagridview1_doubleclick (Objectsender, EventArgs e) {            //the code here simply writes the selected information to the modified groupXiugai_bianhao. Text = datagridview1.selectedcells[0]. Value.tostring (). Trim ();//numberingXiugai_mingzi. Text = datagridview1.selectedcells[1]. Value.tostring (). Trim ();//nameXiugai_xingbie. Text = datagridview1.selectedcells[2]. Value.tostring (). Trim ();//SexXiugai_shengri. Value =datetime.parse (datagridview1.selectedcells[3]. Value.tostring ());//BirthdayXiugai_banji. Text = datagridview1.selectedcells[4]. Value.tostring (). Trim ();//class}

Note: Query the cells in the selected row, whose cells are indexed from left to right, 0,1,2,3, ...

Modify button:

Private voidButton3_Click (Objectsender, EventArgs e) {Dataclasses1datacontext DC=NewDataclasses1datacontext (); varXiu = DC. Student.single (m = M.sno = = Xiugai_bianhao. Text);//determine the row according to the numberXiu. Sname= Xiugai_mingzi. Text.trim ();//Modify NameXiu. Ssex = Xiugai_xingbie. Text.trim ();//SexXiu. Sbirthday = Xiugai_shengri. Value;//BirthdayXiu. Class = Xiugai_banji. Text.trim ();//classDC. SubmitChanges ();//important code Don't miss out        }

Double-click a row:

Then modify the data in a group that modifies the data:

Click Edit to search again.

The modification was successful.

Delete data:

Add Delete button to write code:

 private  void  button4_click (object   sender, EventArgs e) {  dataclasses1datacontext DC  = new   Dataclasses1datacontext ();  var  del = DC. Student.single (m = M.sno = = Datagridview1.selectedcells[0 ]. Value); //  DC. Student.deleteonsubmit (DEL); //  DELETE statement   DC. SubmitChanges ();  //  important code do not omit  
        }

Select a row and click the Delete button to query again:

Delete succeeded.

20150222-linq to SQL Insert, update, and delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.