The following is an example of a SQL-To-SQL CRUD operation.

Source: Internet
Author: User

Yesterday, I briefly looked at the method of writing a simple CRUD operation To SQL. Remove tedious theories and paste code directly. [Source code download]

I created a local database. The database fields are as follows:

 

According to this structure, an entity class Book is designed:

[Table] public class Book {// <summary> // Book ID /// </summary> [Column (IsPrimaryKey = true, DbType = ("bigint "), isDbGenerated = true)] public Int64 bId {get; set ;} /// <summary> /// book name /// </summary> [Column] public string Title {get; set ;} /// <summary >/// unit Price /// </summary> [Column (DbType = "float")] public float Price {get; set ;} /// <summary> /// Author /// </summary> [Column] public string Author {get; set ;} /// <summary> // ISBN No. /// </summary> [Column] public string ISBN {get; set ;} /// <summary> /// the recorded version number /// </summary> [Column (DbType = "rowversion", IsVersion = true)] public byte [] Stamp {get; set ;}}

 

The following is the CRUD operation on it:

# Region Linq To SQL [CRUD operation] private static void LinqToSql_Select () {DataContext context = new DataContext (string. format (@ "Data Source = {0} DB. sdf ", AppDomain. currentDomain. baseDirectory); context. log = Console. out; // output the SQL Execution Process to facilitate tracking and debugging var result = from book in context. getTable <Book> () where book. price> 50 & book. title. contains ("C #") orderby book. price descending select book; Console. writeLine (result. count (); Console. writeLine ("books with a unit price greater than 50 RMB:"); foreach (var item in result) // during traversal, the query {Console is actually executed. writeLine (item. title) ;}} private static void LinqToSql_Insert () {DataContext context = new DataContext (string. format (@ "Data Source = {0} DB. sdf ", AppDomain. currentDomain. baseDirectory); context. log = Console. out; // output the SQL Execution Process to facilitate tracking and debugging ITable books = context. getTable <Book> (); Book book = new Book () {Title = "C # advanced programming", Author = "Wrox", ISBN = "1235 = 23-233 ", price = 138}; books. insertOnSubmit (book); context. submitChanges (); Console. writeLine (book. bId);} private static void LinqToSql_Update () {DataContext context = new DataContext (string. format (@ "Data Source = {0} DB. sdf ", AppDomain. currentDomain. baseDirectory); context. log = Console. out; // the execution process of the output SQL to facilitate tracking and debugging var books = from B in context. getTable <Book> () where B. bId = 5 select B; Book book = books. first <Book> (); book. title = "C # advanced programming (fifth edition)"; context. submitChanges ();} private static void LinqToSql_Delete () {DataContext context = new DataContext (string. format (@ "Data Source = {0} DB. sdf ", AppDomain. currentDomain. baseDirectory); context. log = Console. out; // output the SQL Execution Process to facilitate tracking and debugging Table <Book> books = context. getTable <Book> (); var query = from B in books where B. bId = 5 select B; Book book = query. first <Book> (); books. deleteOnSubmit (book); context. submitChanges () ;}# endregion

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.