Entlib5.0 Data Query

Source: Internet
Author: User

Entlib5.0 It has been out for a long time. In this version, some useful features are provided. Let's take a test today and use Sprocaccessor And Sqlstringaccessor These two classes can return Dataset, datatable Convert a style dataset to an object data.

1. original query method.

Database DB = databasefactory. createdatabase ();

Dbconnection connection = dB. createconnection ();
Dbcommand cmd = connection. createcommand ();
Cmd. commandtype = commandtype. text;
Cmd. commandtext ="Select * From person";
Dataset DS = dB. executedataset (CMD );

(About data connection configuration, please refer to the help documentation, Project address: http://entlib.codeplex.com/

 

2. Use the default ing. The ing between database columns and object data attributes is one-to-one.

Columns in the database

 

Entity type: person

Public Class Person
{
Public Int Personid { Get ; Set ;}
Public String Lastname { Get ; Set ;}
Public String Firstname { Get ; Set ;}
Public Datetime? Hiredate { Get ; Set ;}
Public Datetime? Enrollmentdate { Get ; Set ;}
Public Override String Tostring ()
{
Return String . Format ( @" Personid: {0}, name: {1}-{2} " , Personid, firstname, lastname );
}
}

Next let's take a look at how we query

String Sql1 = " Select * From person " ; // Query all data
Dataaccessor <person> accessor = dB. createsqlstringaccessor <person> (sql1 );
List <person> Persons = accessor. Execute (). tolist <person> ();
Persons. foreach (P =>
{
Console. writeline (P. tostring ());
});

 

3. Custom ing.

Single-row data ing. Considering the actual project, the data format (viewmodel) presented in the previous section may be different from the original model. Here we redefine an object type, it may not have a one-to-one relationship with database columns.

///   <Summary>
/// Data transmission object
///   </Summary>
Public Class Personcoursegradedto
{
Public String Personname { Get ; Set ;}
Public String Coursename { Get ; Set ;}
Public Double Grade { Get ; Set ;}
Public Override String Tostring ()
{
Return String . Format ( @" Personname: {0}, coursename: {1}, grade: {2} " , Personname, coursename, grade );
}
}

At this time, we need to customize a rowmapper.

Public Class Personrowmapper: irowmapper <personcoursegradedto>
{
Public Personcoursegradedto maprow (idatarecord row)
{
Personcoursegradedto DTO = New Personcoursegradedto ();
Int Col = row. getordinal ( " Firstname " );
String Firstname = row. getvalue (COL) = dbnull. value? "" : Row. getvalue (COL). tostring ();
Col = row. getordinal ( " Lastname " );
String Lastname = row. getvalue (COL) = dbnull. value? "" : Row. getvalue (COL). tostring ();
Col = row. getordinal ( " Title " );
String Title = row. getvalue (COL) = dbnull. value? "" : Row. getvalue (COL). tostring ();
Col = row. getordinal ( " Grade " );
Double Grade = row. getvalue (COL) = dbnull. value? 0 : Convert. todouble (row. getvalue (COL ));

Dto. personname = firstname +"-"+ Lastname;
Dto. coursename = title;
Dto. Grade = Grade;
ReturnDto;
}
}

 

How to call it?

Irowmapper <personcoursegradedto> rowmapper = New Personrowmapper ();
Dataaccessor <personcoursegradedto> personcoursegradedtoaccessor = dB. createsqlstringaccessor <personcoursegradedto> (sql3, rowmapper );
List <personcoursegradedto> personcoursegradedtos = personcoursegradedtoaccessor. Execute (). tolist <personcoursegradedto> ();
Personcoursegradedtos. foreach (P =>
{
Console. writeline (P. tostring ());
});

Multi-row data ing

Public Class Personrowsetmapper: iresultsetmapper <personcoursegradedto>
{
Public Ienumerable <personcoursegradedto> mapset (idatareader reader)
{
While (Reader. Read ())
{
Personcoursegradedto DTO = New Personcoursegradedto ();
Int Col = reader. getordinal ( " Firstname " );
String Firstname = reader. getvalue (COL) = dbnull. value? "" : Reader. getvalue (COL). tostring ();
Col = reader. getordinal (" Lastname " );
String Lastname = reader. getvalue (COL) = dbnull. value? "" : Reader. getvalue (COL). tostring ();
Col = reader. getordinal ( " Title " );
String Title = reader. getvalue (COL) = dbnull. value? "" : Reader. getvalue (COL). tostring ();
Col = reader. getordinal ( " Grade " );
Double Grade = reader. getvalue (COL) = dbnull. value? 0 : Convert. todouble (reader. getvalue (COL ));

Dto. personname = firstname +"-"+ Lastname;
Dto. coursename = title;
Dto. Grade = Grade;

Yield ReturnDto;
}
}
}

How to query?

Iresultsetmapper <personcoursegradedto> rowmapper2 = New Personrowsetmapper ();
Dataaccessor <personcoursegradedto> personcoursegradedtoaccessor2 = dB. createsqlstringaccessor <personcoursegradedto> (sql3, rowmapper2 );
List <personcoursegradedto> personcoursegradedtos2 = personcoursegradedtoaccessor2.execute (). tolist <personcoursegradedto> ();
Personcoursegradedtos2.foreach (P =>
{
Console. writeline (P. tostring ());
});

 

Getting startedArticle, For the first time contact with a friend of the enterprise database!

 

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.