在管理NuGet程序包中搜索 Dapper,安装Dapper ORM到要操作数据库的项目中。
类代码如下:
public abstract class DataBase
{
///
///
///
public abstract string ConnectionString { get; }
///
///
///
///
///
///
///
///
public DbParameter CreateParameter(DbCommand cmd, String pName, Object value, System.Data.DbType type)
{
var p = cmd.CreateParameter();
p.ParameterName = pName;
p.Value = (value == null ? DBNull.Value : value);
p.DbType = type;
return p;
}
///
///
///
///
public abstract DbCommand CreateCommand();
///
///
///
///
public abstract DbConnection CreateConnection();
///
/// 返回List
///
///
///
///
///
public List Select(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
var list = Dapper.SqlMapper.Query(conn, sql, paramObject);
return list.ToList();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return null;
}
finally
{
if (conn != null)
conn.Close();
}
}
///
///
///
///
///
///
public List Select(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
var list = Dapper.SqlMapper.Query(conn, sql, paramObject);
return list.ToList();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return null;
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// 获取一条数据
///
///
///
///
public dynamic Single(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
var list = Dapper.SqlMapper.QuerySingleOrDefault(conn, sql, paramObject);
return list;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// 获取一条数据
///
///
///
///
///
public T Single(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
var list = Dapper.SqlMapper.QuerySingleOrDefault(conn, sql, paramObject);
return list;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return default(T);
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// 获取一行一列
///
///
///
///
///
public T ExecuteScalar(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
T t = Dapper.SqlMapper.ExecuteScalar(conn, sql, paramObject);
return t;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return default(T);
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// 返回受影响行数
///
///
///
///
public int Execute(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
int count = Dapper.SqlMapper.Execute(conn, sql, paramObject);
return count;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// ExecuteNonQueryReturnId
///
///
///
///
public int ExecuteNonQueryReturnId(string sql, Object paramObject = null)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
int count = Convert.ToInt32(Dapper.SqlMapper.ExecuteScalar(conn, sql, paramObject));
return count;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
finally
{
if (conn != null)
conn.Close();
}
}
///
/// 自行维护事务和连接
///
///
///
///
///
///
public int ExecuteTran(DbConnection conn, string sql, Object paramObject, DbTransaction transaction)
{
int count = Dapper.SqlMapper.Execute(conn, sql, paramObject, transaction);
return count;
}
///
///
///
///
///
///
///
public List ExecuteStoredProcedure(string sql, Object paramObject)
{
DbConnection conn = null;
try
{
conn = CreateConnection();
conn.Open();
var list = Dapper.SqlMapper.Query(conn, sql, paramObject, null, true, null, System.Data.CommandType.StoredProcedure);
return list.ToList();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return null;
}
finally
{
if (conn != null)
conn.Close();
}
}
}
代码如下:
public class SqlDataBase : DataBase
{
///
///
///
public override string ConnectionString
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
}
}
///
/// 尽少用
///
///
public override DbCommand CreateCommand()
{
return new System.Data.SqlClient.SqlCommand();
}
///
/// 常用
///
///
public override DbConnection CreateConnection()
{
//Sql Server 数据库,更换数据库只需改这里即可
DbConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = ConnectionString;
return conn;
}
}
使用时很简单
举列1(查询操作)代码如下:
var db = new SqlDataBase();
var sql = "select * from auth_user where id = @id";
var model = db.Single(sql, new { id = id });
//当key和变量一样时,new { id = id }可简写为new { id },不一样时不能简写
举列2(非查询操作)代码如下:
var db = new SqlDataBase();
//删除用户
var sql = $@"delete from auth_user where id = @id";
var i = db.Execute(sql, new { id });