Use of LINQ to SQL classes

Source: Internet
Author: User

This article describes the use of LINQ to SQL model classes

EntityFramework is a heavyweight database model, and if you want a simple, lightweight database model, it is recommended that you use the LINQ to SQL class

  1. To create a model class

    在项目中添加linq to sql类,会生成dbml文件,将你需要的表拖到此文件中即可
  2. Querying data

    ="server=localhost;database=my_db;uid=sa;pwd=123";=newDataClasses1DataContext(str);// 创建数据库对象var=fromindb.Productselectitem;// 查询数据
  3. New data

    String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);Product TB= New Product(); //CREATE Table ObjectTB.ProductName = "Charger";TB.Productprice =  -;TB.Produceadressid = 4;DB.Product.InsertOnSubmit(TB); //Generate SQL ScriptDB.SubmitChanges(); //Execute SQL script, save data
  4. modifying data

    String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);varItem=  fromSinch DB.Product where s.ProductID == 7Select S;Item.FirstOrDefault().ProductName = "mobile phone charger";DB.SubmitChanges(); //Execute SQL script, save data
  5. Delete data

    String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);varItem=  fromSinch DB.Product where s.ProductID == 7Select S;DB.Product.DeleteOnSubmit(Item.FirstOrDefault());DB.SubmitChanges(); //Execute SQL script, save data

Use of LINQ to SQL classes

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.