Linq additions and deletions change

Source: Internet
Author: User

Linq additions and deletions change

Linq
A highly integrated data access class that automatically maps the database structure, mapping the table name to the class name,

The full mapping of column names into field names, database data access, can greatly reduce the amount of code.

1. First table to have a primary foreign key relationship

2. Create a database

Add a new item to the LinQ to SQL class named database name

3. Server Explorer--connect to the database (equivalent to the connection string in ADO)

4. Server name:.; Use SQL Sever authentication: Username sa; Select or enter a database name;

Drag the desired table (equivalent to the Live entity class in ADO)

5. Create a data access class

Linqlianxidatacontext con = null; Linqlianxidatacontext---database name +datacontext



}



Property extension

member variables for LINQ are question mark types

C # syntax in question mark (?) Operator refers to a nullable type.

public partial class users//This class is part of the users class {    //Gender attribute extension public    string sexstr     {        get {            string sexs= ' < no > ";            if (this._sex!=null)//First determine whether it is empty            {                sexs = Convert.toboolean (this._sex)? " Male ":" female ";//Convert Boolean Q sizes to Boolean            }            return sexs;}    }  Property extension of a foreign key relationship exists in the National attribute extension public    string nationname     {        get {return this. Nation1.nationname; }//this. Foreign key-related tables. Field name    }

Change and delete

Unlike ADO, the data access class

   Find All  
Public list<users> Select () { return con. Users.tolist ();//con. Table name. ToList (); ToList ()--Find all }
Find a data public from a primary key value public Users selectonly (string name) {
where (LAMDBA expression)--Find condition firstordefault--find return first or empty

} 
//Add Data public bool Insert (Users u)
{
bool B=false;
try{
con. Users.insertonsubmit (U); Adding an operation to a table
con. SubmitChanges (); Perform the Add
B = true;
} catch{}
return B;
}
//delete data public bool Delete (string name) {bool B=false;
//delete before deleting Users u = con. Users.where (a =>a.username==name). FirstOrDefault (); if (U!=null)//has a return value {try {con. Users.deleteonsubmit (u); the parameter of//deleteonsubmit () is an object con. SubmitChanges (); B = true; } catch {}} return B; }
//Modify Data public bool Update (Users u) {bool b = false;
//Find out the pre-modified data Users uu = con. Users.where (A = A.username = = u.username). FirstOrDefault (); if (U = null) {try {
//Use the modified value to assign a value of UU to the value before the change. UserName = U.username; Uu. PassWord = U.password; Uu. Nickname = U.nickname; Uu. Sex = U.sex; Uu. Birthday = U.birthday; Uu. Nation = u.nation; Con. SubmitChanges ();//execute B = true; } catch {}} return B; }



















Linq additions and deletions change

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.