淘先锋技术网

首页 1 2 3 4 5 6 7

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Linq;

using Dapper;

namespace DAL

{

public class DapperHelper

{

///

/// 数据库连接字符串

///

private static readonly string connectionString =

ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

///

/// 查询列表

///

/// 查询的sql

/// 替换参数

///

public static List Query(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.Query(sql, param).ToList();

}

}

///

/// 查询第一个数据

///

///

///

///

public static T QueryFirst(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.QueryFirst(sql, param);

}

}

///

/// 查询第一个数据没有返回默认值

///

///

///

///

public static T QueryFirstOrDefault(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.QueryFirstOrDefault(sql, param);

}

}

///

/// 查询单条数据

///

///

///

///

public static T QuerySingle(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.QuerySingle(sql, param);

}

}

///

/// 查询单条数据没有返回默认值

///

///

///

///

public static T QuerySingleOrDefault(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.QuerySingleOrDefault(sql, param);

}

}

///

/// 增删改

///

///

///

///

public static int Execute(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.Execute(sql, param);

}

}

///

/// Reader获取数据

///

///

///

///

public static IDataReader ExecuteReader(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.ExecuteReader(sql, param);

}

}

///

/// Scalar获取数据

///

///

///

///

public static object ExecuteScalar(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.ExecuteScalar(sql, param);

}

}

///

/// Scalar获取数据

///

///

///

///

public static T ExecuteScalarForT(string sql, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

return con.ExecuteScalar(sql, param);

}

}

///

/// 带参数的存储过程

///

///

///

///

public static List ExecutePro(string proc, object param)

{

using (SqlConnection con = new SqlConnection(connectionString))

{

List list = con.Query(proc,

param,

null,

true,

null,

CommandType.StoredProcedure).ToList();

return list;

}

}

///

/// 事务1 - 全SQL

///

/// 多条SQL

/// param

///

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;

}

}

}

}

///

/// 事务2 - 声明参数

///demo:

///dic.Add("Insert into Users values (@UserName, @Email, @Address)",

/// new { UserName = "jack", Email = "[email protected]", Address = "上海" });

///

/// 多条SQL

/// param

///

public static int ExecuteTransaction(Dictionary 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;

}

}

}

}

}

}