Introduction to use
1. configuration file configuration Good connection string
The value of the connectionname corresponds to the name of the connection string
<appSettings> <add key="ConnectionName"Value="DB"/> </appSettings> <connectionStrings> <!--<add name="DB"connectionstring="Data Source=localhost/xe; User id=hr; PASSWORD=HR;"Providername="oracle.manageddataaccess"/>--> <!--<add name="DB"connectionstring="Host=localhost;database=test;uid=root;pwd=sa123456;charset=utf8;"Providername="MySql.Data.MySqlClient"/>--> <add name="DB"connectionstring="Data source=localhost; Initial catalog=test; Persist Security info=true; User Id=sa; password=123456;"Providername="System.Data.SqlClient"/></connectionstrings>
2. Define a good entity
Public classBook {[Key] [databasegenerated (databasegeneratedoption.identity)] Public int? BookID {Get;Set; } Public stringBookName {Get;Set; } Public int? Authorid {Get;Set; } [Notmapped] Public stringAuthorName {Get;Set; } Public Double? Price {Get;Set; } PublicDatetime? publishdate {Get;Set; } }
Additions and deletions are currently supported by SQL Server, MySQL, Oracle
Increase
using New Dnetcontext ()) { var authorid = db. ADD (new" Zhang San "+true }); Db. ADD (new" from getting started to giving up "20.5, publishdate = DateTime.Now, Authorid = Authorid}); }
Modify
using New Dnetcontext ()) {
int Authorid = db. Getmax<author> (M = (int) m.authorid); Db. Update" Demon Hunter ", m = = M.authorid = = Authorid); }
Delete
using New Dnetcontext ()) { vartrue, q = Q.orderby (M = m.authorid)); var effect = db. Delete (author); int Authorid = db. Getmax<author> (M = (int) m.authorid); Db. Delete<Author> (m = M.authorid = = Authorid); }
Inquire
Single-Table Query
The following SQL formats are supported
Like
Contains, StartsWith, EndsWith
Inch
Contains Collection Class
Upper Lower
ToUpper, ToLower
SQL Character Size comparison
CompareTo, Equals
Charindex InStr
IndexOf
Is null or = ' '
IsNullOrEmpty
Type conversions, time formatting
Tostring
Integer conversions
ToInt32
Trim
TrimStart, TrimEnd, Trim
using(Dnetcontext db =NewDnetcontext ()) { varAuthor = db. getsingle<author> (M =true, q = Q.orderby (M =M.authorid)); Author= db. getsingle<author> (M = M.authorname.contains ("John Doe") && M.isvalid = =true); varAuthors= db. getlist<author> (M = M.authorname.startswith ("Zhang San") && M.isvalid = =true); //Get dynamic typelist<Dynamic> name = db. getdistinctlist<author> (M = M.authorname.startswith ("Harry") && M.isvalid = =true,m=>m.authorname); List<string> name1 = db. Getdistinctlist<author,string> (M = M.authorname.startswith ("Harry") && M.isvalid = =true, M =m.authorname); //Get maximum value varAuthorid = db. Getmax<author> (M = (int) M.authorid); //Dynamic QueryWherebuilder<author>where=NewWherebuilder<author>(); where. and (M=>m.authorname.contains ("Zhang San")); where. and (M = m.authorid==3); where. Or (m=>m.isvalid==true); Db. GetList<Author> (where. Whereexpression); //paging parameters coming from the front deskPagefilter page =NewPagefilter {pageindex=1, pagesize=Ten }; Page. and<Author> (m=>"The Watcher's Sky". Contains (M.authorname)); Page. by<Author> (Q=>q.orderby (m=>m.authorname). OrderByDescending (m=>M.authorid)); Pagedatasource<Author> pagesource= db. Getpage<author>(page); }
Multi-Table Query
using(Dnetcontext db =NewDnetcontext ()) { varBooks = db. Joinquery.leftjoin<book, Author> ((m, n) = M.authorid = = N.authorid && N.isvalid = =true) . fields<book, Author> ((m, n) =New{m, n.authorname}). ORDERBYASC<Book> (M =m.bookname). Select<Book>(); varJoin = db. Joinquery.leftjoin<book, Author> ((m, n) = M.authorid = = N.authorid && N.isvalid = =true) . fields<book, Author> ((m, n) =New{m, n.authorname}). ORDERBYASC<Book> (M =m.bookname); Pagefilter page=Newpagefilter {PageIndex =1, PageSize =Ten};//paging parameters The front desk came .Join. Selectpage<book>(page); }
Support for transactions
using(Dnetcontext db =NewDnetcontext ()) {db. Database.begintransaction (); Try{List<Author> authors =NewList<author>(); for(inti =0; I <= -; i++) {authors. ADD (NewAuthor {authorname ="Test"+ i.tostring (), age = -, IsValid =true }); } db. ADD (authors); Db.DataBase.Commit (); } Catch{db. Database.rollback (); } }
Support for SQL statement queries
using(Dnetcontext db =NewDnetcontext ()) {StringBuilder SQL=NewStringBuilder (); List<DbParameter> parameters =NewList<dbparameter>(); Sql. AppendFormat (@"SELECT {0},a.authorname from book B left joins Author A on A.authorid=b.authorid WHERE", Sqlbuilder.getselectallfields<book> ("B")); Sql. Append ("b.bookid> @BookID"); Parameters. ADD (db. Getdbparameter ("BookID",1)); Pagedatasource<Book> books = db. getpage<book> (SQL. ToString (),NewPagefilter {pageindex=1, pagesize=5}, Parameters. ToArray ()); List<Book> BKS = db. Getlist<book>(SQL. ToString (), parameters. ToArray ()); }
Welcome to the technical discussion
Latest code Download
Https://files.cnblogs.com/files/DNetORM/DNet.ORM4.0.rar
Dnetorm a lightweight ORM framework