初始化项目
This commit is contained in:
75
AUTS.Services/DBUtility/CommandInfo.cs
Normal file
75
AUTS.Services/DBUtility/CommandInfo.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AUTS.Services.DBUtility
|
||||
{
|
||||
public enum EffentNextType
|
||||
{
|
||||
/// <summary>
|
||||
/// 对其他语句无任何影响
|
||||
/// </summary>
|
||||
None,
|
||||
/// <summary>
|
||||
/// 当前语句必须为"select count(1) from .."格式,如果存在则继续执行,不存在回滚事务
|
||||
/// </summary>
|
||||
WhenHaveContine,
|
||||
/// <summary>
|
||||
/// 当前语句必须为"select count(1) from .."格式,如果不存在则继续执行,存在回滚事务
|
||||
/// </summary>
|
||||
WhenNoHaveContine,
|
||||
/// <summary>
|
||||
/// 当前语句影响到的行数必须大于0,否则回滚事务
|
||||
/// </summary>
|
||||
ExcuteEffectRows,
|
||||
/// <summary>
|
||||
/// 引发事件-当前语句必须为"select count(1) from .."格式,如果不存在则继续执行,存在回滚事务
|
||||
/// </summary>
|
||||
SolicitationEvent
|
||||
}
|
||||
public class CommandInfo
|
||||
{
|
||||
public object ShareObject = null;
|
||||
public object OriginalData = null;
|
||||
event EventHandler _solicitationEvent;
|
||||
public event EventHandler SolicitationEvent
|
||||
{
|
||||
add
|
||||
{
|
||||
_solicitationEvent += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
_solicitationEvent -= value;
|
||||
}
|
||||
}
|
||||
public void OnSolicitationEvent()
|
||||
{
|
||||
if (_solicitationEvent != null)
|
||||
{
|
||||
_solicitationEvent(this, new EventArgs());
|
||||
}
|
||||
}
|
||||
public string CommandText;
|
||||
public System.Data.Common.DbParameter[] Parameters;
|
||||
public EffentNextType EffentNextType = EffentNextType.None;
|
||||
public CommandInfo()
|
||||
{
|
||||
|
||||
}
|
||||
public CommandInfo(string sqlText, MySqlParameter[] para)
|
||||
{
|
||||
this.CommandText = sqlText;
|
||||
this.Parameters = para;
|
||||
}
|
||||
public CommandInfo(string sqlText, MySqlParameter[] para, EffentNextType type)
|
||||
{
|
||||
this.CommandText = sqlText;
|
||||
this.Parameters = para;
|
||||
this.EffentNextType = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
324
AUTS.Services/DBUtility/Common/DALHelper.cs
Normal file
324
AUTS.Services/DBUtility/Common/DALHelper.cs
Normal file
@@ -0,0 +1,324 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AUTS.Services.DBUtility.Common
|
||||
{
|
||||
public class DALHelper<TEntity> where TEntity : class, new()
|
||||
{
|
||||
private string databasetablename; //数据库表名前缀
|
||||
|
||||
//private DbConfigsInfo confInfi;
|
||||
|
||||
public DALHelper()
|
||||
{
|
||||
databasetablename = "TBL_UTS_Manage";
|
||||
}
|
||||
public DALHelper(string _databaseprefix)
|
||||
{
|
||||
databasetablename = _databaseprefix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在该记录
|
||||
/// </summary>
|
||||
public bool Exists(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select count(1) from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
return DbHelperMySQL.Exists(strSql.ToString(), parameters);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 得到最大ID
|
||||
/// </summary>
|
||||
public int GetMaxId(TEntity entity, string strField)
|
||||
{
|
||||
return DbHelperMySQL.GetMaxID(strField, databasetablename);
|
||||
}
|
||||
|
||||
public bool Add(TEntity entity)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("insert into " + databasetablename + "(");
|
||||
|
||||
PropertyInfo[] propertys = entity.GetType().GetProperties();// 获得此模型的公共属性
|
||||
|
||||
List<MySqlParameter> parameters = new List<MySqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + ",");
|
||||
strSql.Append(" values (");
|
||||
}
|
||||
strSql.Append(" ) values (");
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + "@,");
|
||||
parameters.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(entity)));
|
||||
}
|
||||
strSql.Append(");select @@IDENTITY");
|
||||
int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters.ToArray());
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改一个实体数据
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="strField"></param>
|
||||
/// <param name="keyValue"></param>
|
||||
/// <returns></returns>
|
||||
public bool Update(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("update " + databasetablename + " set ");
|
||||
|
||||
PropertyInfo[] propertys = entity.GetType().GetProperties();// 获得此模型的公共属性
|
||||
|
||||
List<MySqlParameter> parameters = new List<MySqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + "=@" + pi.Name + ",");
|
||||
parameters.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(entity)));
|
||||
}
|
||||
strSql.Append(" where " + strField + "=@strValue");
|
||||
parameters.Add(new MySqlParameter("@strValue", keyValue));
|
||||
int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters.ToArray());
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改一列数据
|
||||
/// </summary>
|
||||
public int UpdateField(TEntity entity, string strField, object strValue, string strWhere)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("update " + databasetablename);
|
||||
strSql.Append(" set " + strField + "=@strValue");
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@strValue",strValue)
|
||||
};
|
||||
|
||||
return DbHelperMySQL.ExecuteSql(strSql.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除一条数据
|
||||
/// </summary>
|
||||
public bool Delete(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("delete from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
|
||||
int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到一个对象实体
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <returns></returns>
|
||||
public TEntity GetModel(TEntity entity, string strField, string keyValue)
|
||||
{
|
||||
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select * from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
strSql.Append(" LIMIT 0,1;");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
|
||||
DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModel(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<TEntity> GetList(string strWhere = null)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select * from " + databasetablename);
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
//strSql.Append(" LIMIT 0,1;");
|
||||
TEntity entity = new TEntity();
|
||||
DataSet ds = DbHelperMySQL.Query(strSql.ToString());
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModels(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将DataTable转换得到一个对象实体
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public TEntity DataRowToModel(TEntity model, DataTable dt)
|
||||
{
|
||||
if (dt.Rows.Count > 0)
|
||||
{
|
||||
TEntity t = new TEntity();
|
||||
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (dt.Columns.Contains(pi.Name))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
var value = dt.Rows[0][pi.Name];
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
switch (pi.PropertyType.FullName)
|
||||
{
|
||||
case "System.Decimal":
|
||||
pi.SetValue(t, decimal.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.String":
|
||||
pi.SetValue(t, value.ToString(), null);
|
||||
break;
|
||||
case "System.Single":
|
||||
pi.SetValue(t, float.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Double":
|
||||
pi.SetValue(t, double.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Int32":
|
||||
pi.SetValue(t, int.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.DateTime":
|
||||
pi.SetValue(t, DateTime.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Boolean":
|
||||
pi.SetValue(t, bool.Parse(value.ToString()), null);
|
||||
break;
|
||||
default:
|
||||
pi.SetValue(t, value, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将DataTable转换得到一个对象实体集合
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> DataRowToModels(TEntity model, DataTable dt)
|
||||
{
|
||||
List<TEntity> ts = new List<TEntity>();// 定义集合
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
TEntity t = new TEntity();
|
||||
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (dt.Columns.Contains(pi.Name))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
var value = dr[pi.Name];
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
switch (pi.PropertyType.FullName)
|
||||
{
|
||||
case "System.Decimal":
|
||||
pi.SetValue(t, decimal.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.String":
|
||||
pi.SetValue(t, value.ToString(), null);
|
||||
break;
|
||||
case "System.Single":
|
||||
pi.SetValue(t, float.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Double":
|
||||
pi.SetValue(t, double.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Int32":
|
||||
pi.SetValue(t, int.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.DateTime":
|
||||
pi.SetValue(t, DateTime.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Boolean":
|
||||
pi.SetValue(t, bool.Parse(value.ToString()), null);
|
||||
break;
|
||||
default:
|
||||
pi.SetValue(t, value, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.Add(t);
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
1230
AUTS.Services/DBUtility/Common/DbHelperMySQL.cs
Normal file
1230
AUTS.Services/DBUtility/Common/DbHelperMySQL.cs
Normal file
File diff suppressed because it is too large
Load Diff
524
AUTS.Services/DBUtility/Custom/DALHelperCustom.cs
Normal file
524
AUTS.Services/DBUtility/Custom/DALHelperCustom.cs
Normal file
@@ -0,0 +1,524 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AUTS.Services.DBUtility.Custom
|
||||
{
|
||||
public class DALHelperCustom<TEntity> where TEntity : class, new()
|
||||
{
|
||||
private string databasetablename; //数据库表名前缀
|
||||
|
||||
//private DbConfigsInfo confInfi;
|
||||
|
||||
public DALHelperCustom()
|
||||
{
|
||||
databasetablename = "TBL_UTS_Manage";
|
||||
}
|
||||
public DALHelperCustom(string _databaseprefix)
|
||||
{
|
||||
databasetablename = _databaseprefix;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否存在该记录
|
||||
/// </summary>
|
||||
public bool Exists(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select count(1) from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
return DbHelperMySqlCustom.Exists(strSql.ToString(), parameters);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 得到最大ID
|
||||
/// </summary>
|
||||
public int GetMaxId(TEntity entity, string strField)
|
||||
{
|
||||
return DbHelperMySqlCustom.GetMaxID(strField, databasetablename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一条数据
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public bool Add(TEntity entity)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("insert into " + databasetablename + "(");
|
||||
|
||||
PropertyInfo[] propertys = entity.GetType().GetProperties();// 获得此模型的公共属性
|
||||
|
||||
List<MySqlParameter> parameters = new List<MySqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite || pi.GetValue(entity)==null) continue;
|
||||
strSql.Append(pi.Name + ",");
|
||||
}
|
||||
strSql.Remove(strSql.Length - 1, 1);//移除最后一个逗号
|
||||
strSql.Append(" ) values (");
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite || pi.GetValue(entity) == null) continue;
|
||||
strSql.Append("@" + pi.Name + ",");
|
||||
parameters.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(entity)));
|
||||
}
|
||||
strSql.Remove(strSql.Length - 1, 1);//移除最后一个逗号
|
||||
strSql.Append(");select @@IDENTITY");
|
||||
int rows = DbHelperMySqlCustom.ExecuteSql(strSql.ToString(), parameters.ToArray());
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改一列数据
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="strField"></param>
|
||||
/// <param name="keyValue"></param>
|
||||
/// <returns></returns>
|
||||
public bool Update(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("update " + databasetablename + " set ");
|
||||
|
||||
PropertyInfo[] propertys = entity.GetType().GetProperties();// 获得此模型的公共属性
|
||||
|
||||
List<MySqlParameter> parameters = new List<MySqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + "=@" + pi.Name + ",");
|
||||
parameters.Add(new MySqlParameter("@" + pi.Name, pi.GetValue(entity)));
|
||||
}
|
||||
strSql.Remove(strSql.Length - 1, 1);//移除最后一个逗号
|
||||
strSql.Append(" where " + strField + "=@strValue");
|
||||
|
||||
System.Type keyType = keyValue.GetType();
|
||||
switch (keyType.Name)
|
||||
{
|
||||
case "Int32":
|
||||
MySqlParameter strValue = new MySqlParameter("@strValue", MySqlDbType.Int32)
|
||||
{
|
||||
Value = keyValue
|
||||
};
|
||||
parameters.Add(strValue);
|
||||
break;
|
||||
default:
|
||||
parameters.Add(new MySqlParameter("@strValue", keyValue));
|
||||
break;
|
||||
}
|
||||
|
||||
int rows = DbHelperMySqlCustom.ExecuteSql(strSql.ToString(), parameters.ToArray());
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改一列数据
|
||||
/// </summary>
|
||||
public int UpdateField(TEntity entity, string strField, object strValue, string strWhere)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("update " + databasetablename);
|
||||
strSql.Append(" set " + strField + "=@strValue");
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@strValue",strValue)
|
||||
};
|
||||
|
||||
return DbHelperMySqlCustom.ExecuteSql(strSql.ToString(), parameters);
|
||||
}
|
||||
/// <summary>
|
||||
/// 修改一列数据
|
||||
/// </summary>
|
||||
public int UpdateFieldTia(TEntity entity, string strField, string strWhere)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("update " + databasetablename);
|
||||
strSql.Append(" set " + strField);
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
|
||||
|
||||
return DbHelperMySqlCustom.ExecuteSql(strSql.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除一条数据
|
||||
/// </summary>
|
||||
public bool Delete(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("delete from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
|
||||
int rows = DbHelperMySqlCustom.ExecuteSql(strSql.ToString(), parameters);
|
||||
if (rows > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到一个对象实体
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <returns></returns>
|
||||
public TEntity GetModel(TEntity entity, string strField, object keyValue)
|
||||
{
|
||||
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select * from " + databasetablename);
|
||||
strSql.Append(" where " + strField + "=@keyValue ");
|
||||
strSql.Append(" LIMIT 0,1;");
|
||||
MySqlParameter[] parameters = {
|
||||
new MySqlParameter("@keyValue",keyValue)
|
||||
};
|
||||
|
||||
DataSet ds = DbHelperMySqlCustom.Query(strSql.ToString(), parameters);
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModel(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 得到数据列表
|
||||
/// </summary>
|
||||
/// <param name="strWhere"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> GetList(string strWhere = null)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select * from " + databasetablename);
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
//strSql.Append(" LIMIT 0,1;");
|
||||
//strSql.Append(";");
|
||||
TEntity entity = new TEntity();
|
||||
DataSet ds = DbHelperMySqlCustom.Query(strSql.ToString());
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModels(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<TEntity>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重载,得到数据列表
|
||||
/// </summary>
|
||||
/// <param name="columnName"></param>
|
||||
/// <param name="strWhere"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> GetList(string columnName, string strWhere)
|
||||
{
|
||||
StringBuilder strSql = new StringBuilder();
|
||||
strSql.Append("select " + columnName + " from " + databasetablename);
|
||||
if (!string.IsNullOrEmpty(strWhere))
|
||||
{
|
||||
strSql.Append(" where " + strWhere);
|
||||
}
|
||||
//strSql.Append(" LIMIT 0,1;");
|
||||
strSql.Append(";");
|
||||
TEntity entity = new TEntity();
|
||||
DataSet ds = DbHelperMySqlCustom.Query(strSql.ToString());
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModels(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到数据列表
|
||||
/// </summary>
|
||||
/// <param name="strSql"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> SqlQueryGetList(string strSql)
|
||||
{
|
||||
//StringBuilder strSql = new StringBuilder();
|
||||
//strSql.Append("select " + columnName + " from " + databasetablename);
|
||||
//if (!string.IsNullOrEmpty(strWhere))
|
||||
//{
|
||||
// strSql.Append(" where " + strWhere);
|
||||
//}
|
||||
//strSql.Append(" LIMIT 0,1;");
|
||||
//strSql.Append(";");
|
||||
TEntity entity = new TEntity();
|
||||
DataSet ds = DbHelperMySqlCustom.Query(strSql);
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModels(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 得到数据列表
|
||||
/// </summary>
|
||||
/// <param name="strSql"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> SqlQueryGetListdb(string strSql)
|
||||
{
|
||||
//StringBuilder strSql = new StringBuilder();
|
||||
//strSql.Append("select " + columnName + " from " + databasetablename);
|
||||
//if (!string.IsNullOrEmpty(strWhere))
|
||||
//{
|
||||
// strSql.Append(" where " + strWhere);
|
||||
//}
|
||||
//strSql.Append(" LIMIT 0,1;");
|
||||
//strSql.Append(";");
|
||||
TEntity entity = new TEntity();
|
||||
DataSet ds = DbHelperMySqlCustom.dbQuery(strSql);
|
||||
if (ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return DataRowToModels(entity, ds.Tables[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将DataTable转换得到一个对象实体
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public TEntity DataRowToModel(TEntity model, DataTable dt)
|
||||
{
|
||||
if (dt.Rows.Count > 0)
|
||||
{
|
||||
TEntity t = new TEntity();
|
||||
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (dt.Columns.Contains(pi.Name))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
var value = dt.Rows[0][pi.Name];
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
switch (pi.PropertyType.FullName)
|
||||
{
|
||||
case "System.Decimal":
|
||||
pi.SetValue(t, decimal.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.String":
|
||||
pi.SetValue(t, value.ToString(), null);
|
||||
break;
|
||||
case "System.Single":
|
||||
pi.SetValue(t, float.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Double":
|
||||
pi.SetValue(t, double.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Int32":
|
||||
pi.SetValue(t, int.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.DateTime":
|
||||
pi.SetValue(t, DateTime.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Boolean":
|
||||
pi.SetValue(t, bool.Parse(value.ToString()), null);
|
||||
break;
|
||||
default:
|
||||
pi.SetValue(t, value, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将DataTable转换得到一个对象实体集合
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> DataRowToModels(TEntity model, DataTable dt)
|
||||
{
|
||||
List<TEntity> ts = new List<TEntity>();// 定义集合
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
TEntity t = new TEntity();
|
||||
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (dt.Columns.Contains(pi.Name))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
var value = dr[pi.Name];
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
switch (pi.PropertyType.FullName)
|
||||
{
|
||||
case "System.Decimal":
|
||||
pi.SetValue(t, decimal.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.String":
|
||||
pi.SetValue(t, value.ToString(), null);
|
||||
break;
|
||||
case "System.Single":
|
||||
pi.SetValue(t, float.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Double":
|
||||
pi.SetValue(t, double.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Int32":
|
||||
pi.SetValue(t, int.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.DateTime":
|
||||
pi.SetValue(t, DateTime.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Boolean":
|
||||
pi.SetValue(t, bool.Parse(value.ToString()), null);
|
||||
break;
|
||||
default:
|
||||
pi.SetValue(t, value, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.Add(t);
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将DataTable转换得到一个对象实体集合
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public List<TEntity> DataRowToModels(DataTable dt)
|
||||
{
|
||||
List<TEntity> ts = new List<TEntity>();// 定义集合
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
try
|
||||
{
|
||||
TEntity t = new TEntity();
|
||||
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (dt.Columns.Contains(pi.Name))
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
var value = dr[pi.Name];
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
switch (pi.PropertyType.FullName)
|
||||
{
|
||||
case "System.Decimal":
|
||||
pi.SetValue(t, decimal.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.String":
|
||||
pi.SetValue(t, value.ToString(), null);
|
||||
break;
|
||||
case "System.Single":
|
||||
pi.SetValue(t, float.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Double":
|
||||
pi.SetValue(t, double.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Int32":
|
||||
if (value.ToString().Trim().Length <= 0)
|
||||
{
|
||||
value = "0";
|
||||
}
|
||||
pi.SetValue(t, int.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.DateTime":
|
||||
pi.SetValue(t, DateTime.Parse(value.ToString()), null);
|
||||
break;
|
||||
case "System.Boolean":
|
||||
pi.SetValue(t, bool.Parse(value.ToString()), null);
|
||||
break;
|
||||
default:
|
||||
pi.SetValue(t, value, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.Add(t);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
continue;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
}
|
||||
1321
AUTS.Services/DBUtility/Custom/DbHelperMySqlCustom.cs
Normal file
1321
AUTS.Services/DBUtility/Custom/DbHelperMySqlCustom.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user