Use basic dapper, a lightweight ORM framework that's handy

Source: Internet
Author: User

public class Dbhelper<t>where T:class
{
<summary>
Database connection string
</summary>
public static readonly String connectionString = configurationmanager.connectionstrings["constring_string"]. ConnectionString;


#region Search
<summary>
Query list
</summary>
<param name= "SQL" > Query sql</param>
<param name= "param" > Replace parameters </param>
<returns></returns>
public static list<t> Query (String sql, Object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
Return con. query<t> (SQL, param). ToList ();
}
}

<summary>
Querying the first Data
</summary>
<param name= "SQL" ></param>
<param name= "param" ></param>
<returns></returns>
public static T Queryfirst (String sql, Object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
Return con. queryfirst<t> (SQL, param);
}
}

<summary>
Query the first data does not return a default value
</summary>
<param name= "SQL" ></param>
<param name= "param" ></param>
<returns></returns>
public static T Queryfirstordefault (String sql, Object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
Return con. queryfirstordefault<t> (SQL, param);
}
}

//<summary>
//Query single data
//</SUMMARY>
//<param name= "SQL" ></PARAM>
//&L T;param name= "param" ></PARAM>
//<returns></returns>
public static T Querysingle (string SQL, object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
return con. querysingle<t> (SQL, param);
}
}
//<summary>
///Query single data does not return a default value
//</summary>
//<param name= "SQL" ></ Param>
//<param name= "param" ></PARAM>
//<returns></returns>
public static T Querysingleordefault (String sql, object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
return con. querysingleordefault<t> (SQL, param);
}
}
#endregion

#region Update, Create, Delete
//<summary>
////</summary>
//<param name= "sql "></PARAM>
//<param name=" param "></PARAM>
//<returns></returns>
public static int Execute (String sql, object param)
{
using (SqlConnection con = new SqlConnection (Connectionstrin g))
{
return con. Execute (SQL, param);
}
}
#endregion

#region Storage
<summary>
Stored procedure with parameters
</summary>
<param name= "SQL" ></param>
<param name= "param" ></param>
<returns></returns>
public static list<t> Executepro (string proc, object param)
{
using (SqlConnection con = new SqlConnection (connectionString))
{
list<t> list = con. Query<t> (proc,
Param
Null
True
Null
CommandType.StoredProcedure). ToList ();
return list;
}
}
#endregion
}

The NuGet package using dapper currently supports. NET fromwork4.51 at its lowest level, so it feels more personal than ADO and easy to learn.

public void Search ()
{
var id = 1;
string query = "SELECT * from member where [email protected]";
var data = Dbhelper<member>. Query (query, new {id=id});

This. Response.Write ((object) data);
This. Response.End ();
}

The member above is a solid object, and if only the entity object member part of the field is required, you can selectively create

I record the province after the search everywhere.

Use basic dapper, a lightweight ORM framework that's handy

Related Article

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.