using
Dapper;
using
System;
using
System.Collections.Generic;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
PullChargeData.Helper
{
public
class
DapperHelper<T>
{
/// <summary>
/// Database connection string
/// </summary>
private
static
readonly
string
connectionString = ConfigurationManager.ConnectionStrings[
"db"
].ConnectionString;
/// <summary>
/// Query list (return to DataTable)
/// </summary>
/// <returns></returns>
public
static
DataTable QueryToDataTable(
string
sql)
{
DataTable table =
new
DataTable(
"MyTable"
);
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
var
reader = con.ExecuteReader(sql);
table.Load(reader);
return
table;
}
}
/// <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 =
null
)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Query<T>(sql, param).ToList();
}
}
/// <summary>
/// Query the first data
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
T QueryFirst(
string
sql,
object
param =
null
)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Query<T>(sql, param).ToList().First();
}
}
/// <summary>
/// The first data in the query did not return the default value
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
T QueryFirstOrDefault(
string
sql,
object
param =
null
)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Query<T>(sql, param).ToList().FirstOrDefault();
}
}
/// <summary>
/// Query a single data
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
T QuerySingle(
string
sql,
object
param =
null
)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Query<T>(sql, param).ToList().Single();
}
}
/// <summary>
/// Query a single piece of data does not return the default value
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
T QuerySingleOrDefault(
string
sql,
object
param =
null
)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Query<T>(sql, param).ToList().SingleOrDefault();
}
}
/// <summary>
/// Addition, deletion and modification
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns>Number of rows affected</returns>
public
static
int
Execute(
string
sql,
object
param)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.Execute(sql, param);
}
}
/// <summary>
/// Reader gets data
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
IDataReader ExecuteReader(
string
sql,
object
param)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.ExecuteReader(sql, param);
}
}
/// <summary>
/// Scalar gets data
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
object
ExecuteScalar(
string
sql,
object
param)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.ExecuteScalar(sql, param);
}
}
/// <summary>
/// Scalar gets data
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public
static
T ExecuteScalarForT(
string
sql,
object
param)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
return
con.ExecuteScalar<T>(sql, param);
}
}
/// <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;
}
}
/// <summary>
/// Transaction 1-Full SQL
/// </summary>
/// <param name="sqlarr">Multiple SQL</param>
/// <param name="param">param</param>
/// <returns></returns>
public
static
int
ExecuteTransaction(
string
[] sqlarr)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
using
(
var
transaction = con.BeginTransaction())
{
try
{
int
result = 0;
foreach
(
var
sql
in
sqlarr)
{
result += con.Execute(sql,
null
, transaction);
}
transaction.Commit();
return
result;
}
catch
(Exception ex)
{
transaction.Rollback();
return
0;
}
}
}
}
/// <summary>
/// Transaction 2-declare parameters
///demo:
///dic.Add("Insert into Users values (@UserName, @Email, @Address)",
/// new {UserName = "jack", Email = "380234234@qq.com", Address = "Shanghai" });
/// </summary>
/// <param name="Key">Multiple SQL</param>
/// <param name="Value">param</param>
/// <returns></returns>
public
static
int
ExecuteTransaction(Dictionary<
string
,
object
> dic)
{
using
(SqlConnection con =
new
SqlConnection(connectionString))
{
using
(
var
transaction = con.BeginTransaction())
{
try
{
int
result = 0;
foreach
(
var
sql
in
dic)
{
result += con.Execute(sql.Key, sql.Value, transaction);
}
transaction.Commit();
return
result;
}
catch
(Exception ex)
{
transaction.Rollback();
return
0;
}
}
}
}
}
}