196 lines
6.1 KiB
C#
196 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Reflection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hncore.Infrastructure.EF
|
|
{
|
|
public static class Sql
|
|
{
|
|
/// <summary>
|
|
/// 执行Reader
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="sql"></param>
|
|
/// <param name="action"></param>
|
|
public static void Reader(this DbContext dbContext, string sql, Action<DbDataReader> action)
|
|
{
|
|
var conn = dbContext.Database.GetDbConnection();
|
|
|
|
try
|
|
{
|
|
conn.Open();
|
|
|
|
using (var command = conn.CreateCommand())
|
|
{
|
|
string query = sql;
|
|
|
|
command.CommandText = query;
|
|
|
|
using (DbDataReader reader = command.ExecuteReader())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
action(reader);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行Query
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="sql"></param>
|
|
/// <returns></returns>
|
|
public static List<T> SqlQuery<T>(this DbContext dbContext, string sql)
|
|
{
|
|
List<T> list = new List<T>();
|
|
|
|
var conn = dbContext.Database.GetDbConnection();
|
|
|
|
try
|
|
{
|
|
conn.Open();
|
|
|
|
using (var command = conn.CreateCommand())
|
|
{
|
|
string query = sql;
|
|
|
|
command.CommandText = query;
|
|
|
|
using (DbDataReader reader = command.ExecuteReader())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
list = reader.ReaderToList<T>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行Sql命令
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="sql"></param>
|
|
/// <returns></returns>
|
|
public static int ExecuteSql(this DbContext dbContext, string sql)
|
|
{
|
|
var conn = dbContext.Database.GetDbConnection();
|
|
int rowAffected = 0;
|
|
try
|
|
{
|
|
conn.Open();
|
|
|
|
using (var command = conn.CreateCommand())
|
|
{
|
|
command.CommandText = sql;
|
|
rowAffected = command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
|
|
return rowAffected;
|
|
}
|
|
|
|
/// <summary>
|
|
/// DataReader转泛型
|
|
/// </summary>
|
|
/// <typeparam name="T">传入的实体类</typeparam>
|
|
/// <param name="objReader">DataReader对象</param>
|
|
/// <returns></returns>
|
|
public static List<T> ReaderToList<T>(this DbDataReader objReader)
|
|
{
|
|
using (objReader)
|
|
{
|
|
List<T> list = new List<T>();
|
|
|
|
//获取传入的数据类型
|
|
Type modelType = typeof(T);
|
|
|
|
//遍历DataReader对象
|
|
while (objReader.Read())
|
|
{
|
|
//使用与指定参数匹配最高的构造函数,来创建指定类型的实例
|
|
T model = Activator.CreateInstance<T>();
|
|
for (int i = 0; i < objReader.FieldCount; i++)
|
|
{
|
|
//判断字段值是否为空或不存在的值
|
|
if (!IsNullOrDBNull(objReader[i]))
|
|
{
|
|
//匹配字段名
|
|
PropertyInfo pi = modelType.GetProperty(objReader.GetName(i),
|
|
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance |
|
|
BindingFlags.IgnoreCase);
|
|
if (pi != null)
|
|
{
|
|
//绑定实体对象中同名的字段
|
|
pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
|
|
}
|
|
}
|
|
}
|
|
|
|
list.Add(model);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断指定对象是否是有效值
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
private static bool IsNullOrDBNull(object obj)
|
|
{
|
|
return (obj == null || (obj is DBNull)) ? true : false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对可空类型进行判断转换
|
|
/// </summary>
|
|
/// <param name="value">DataReader字段的值</param>
|
|
/// <param name="conversionType">该字段的类型</param>
|
|
/// <returns></returns>
|
|
private static object CheckType(object value, Type conversionType)
|
|
{
|
|
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
|
|
{
|
|
if (value == null)
|
|
return null;
|
|
System.ComponentModel.NullableConverter nullableConverter =
|
|
new System.ComponentModel.NullableConverter(conversionType);
|
|
conversionType = nullableConverter.UnderlyingType;
|
|
}
|
|
|
|
if (typeof(System.Enum).IsAssignableFrom(conversionType))
|
|
{
|
|
return Enum.Parse(conversionType, value.ToString());
|
|
}
|
|
return Convert.ChangeType(value, conversionType);
|
|
}
|
|
}
|
|
} |