WinForm using LINQ to SQL class to implement database add, delete, change, check February 18, 2016

Source: Internet
Author: User

In C #, if you are using a SQL Server database, you can use the LINQ to SQL class to make operations on the database easier because of the same Microsoft-owned software.

One, add a LINQ to SQL class:

Second, in the data connection in the Resource Manager, connect the database:

Third, the table to be operated, dragged into the DATACLASSES1.DBML operator panel, so that the table properties are encapsulated

Four, create a FRUITDA class, in the inside write the database to add, delete, change, check the method, the code is as follows:

  Public classFRUITDA {PrivateDataclasses1datacontext Context;//create a LINQ to SQL Class object context, data Model        PublicFruitda () {Context=NewDataclasses1datacontext ();//constructor to instantiate the context inside       }        Public voidInsert (Fruit data)//Add Method{Context.Fruit.InsertOnSubmit (data);//adding data to the fruit tableContext.submitchanges ();//have changed, commit to database       }        Public voidUpdate (Fruit data)//Modify Method       {            //query First, then modifyFruit sdata = Context.Fruit.Where (r = R.ids = =data. IDS).           First (); //The equal sign greater than sign is an LAMDA expression, and the equals sign is equal to the in list of the list generic collection//R can be arbitrarily named, representing a row of data, the equivalent of fruit data in Data,foreach each row is a row of data, a row of data is//If they are equal, if they are not equal, go to the next bar.//after the filter is finished, take the data from the first one, and return an object of type fruit.           if(Sdata! =NULL)//Judging if the data is not empty, follow the sdata data to modify the data{sdata. Name=data.               Name; Sdata. Price=data.               Price; Sdata. Numbers=data.               Numbers; Sdata. Source=data.               Source; Sdata. Stack=data.               Stack; Sdata. Image=data.           Image; } context.submitchanges ();//Modify commit to Database       }        Public voidDelete (Fruit data)//Delete Method{Context.Fruit.DeleteOnSubmit (data);       Context.submitchanges (); }        PublicList<fruit> Select ()//Query method that returns a generic collection of type fruit       {           returnContext.Fruit.ToList ();//Convert the result to a generic collection using the ToList function       }        PublicList<fruit> Selectbyname (stringName//Fuzzy Query       {           returnContext.Fruit.Where (r=>r.name.contains (Name)).           ToList (); //See if the filtered line has a name like name, with a contains package that does not contain the defined lower case, and then converts to a generic collection to return       }        Public BOOLCheckfruit (stringNamedecimalPrice//to verify the style of the user name password       {           returnContext.Fruit.Where (r = r.name = = Name && R.price = = Price). Count () >0; //filter multiple conditions at the same time to satisfy the &&, and the number of bars that satisfy the condition data with Count//if greater than 0 returns true, less than or equal to zero returns false       }        PublicFruitSelect(stringIDs//querying a single piece of data       {           returnContext.Fruit.Single (r = R.ids = =IDs); //If the query is a primary key, you can use single//not a primary key, use where to filter       }    }

The test code is as follows:

Private void Form1_Load (object  sender, EventArgs e)        {            Listnew  FRUITDA (). Select ();             = list;        }

WinForm using LINQ to SQL class to implement database add, delete, change, check February 18, 2016

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.