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