初始化
This commit is contained in:
76
Face.Services/DBUtility/CommandInfo.cs
Normal file
76
Face.Services/DBUtility/CommandInfo.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.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, SqlParameter[] para)
|
||||
{
|
||||
this.CommandText = sqlText;
|
||||
this.Parameters = para;
|
||||
}
|
||||
public CommandInfo(string sqlText, SqlParameter[] para, EffentNextType type)
|
||||
{
|
||||
this.CommandText = sqlText;
|
||||
this.Parameters = para;
|
||||
this.EffentNextType = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
357
Face.Services/DBUtility/Common/DALHelper.cs
Normal file
357
Face.Services/DBUtility/Common/DALHelper.cs
Normal file
@@ -0,0 +1,357 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.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<SqlParameter> parameters = new List<SqlParameter>();
|
||||
// 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 SqlParameter("@" + 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<SqlParameter> parameters = new List<SqlParameter>();
|
||||
// foreach (PropertyInfo pi in propertys)
|
||||
// {
|
||||
// if (!pi.CanWrite) continue;
|
||||
// strSql.Append(pi.Name + "=@" + pi.Name + ",");
|
||||
// parameters.Add(new SqlParameter("@" + 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);
|
||||
}
|
||||
SqlParameter[] parameters = {
|
||||
new SqlParameter("@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>
|
||||
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>MySqlDataReader</returns>
|
||||
//public static void ExecuteReader(string strSQL, Action<MySqlDataReader> func)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
// {
|
||||
// using (MySqlCommand cmd = new MySqlCommand(strSQL, connection))
|
||||
// {
|
||||
// connection.Open();
|
||||
|
||||
// using (MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
|
||||
// {
|
||||
// func(myReader);
|
||||
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
// catch (MySqlException e)
|
||||
// {
|
||||
// throw e;
|
||||
// }
|
||||
|
||||
//}
|
||||
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
1208
Face.Services/DBUtility/Common/DbHelperMySQL.cs
Normal file
1208
Face.Services/DBUtility/Common/DbHelperMySQL.cs
Normal file
File diff suppressed because it is too large
Load Diff
356
Face.Services/DBUtility/Common/MysqlHelpers.cs
Normal file
356
Face.Services/DBUtility/Common/MysqlHelpers.cs
Normal file
@@ -0,0 +1,356 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Services.DBUtility.Common
|
||||
{
|
||||
public class MysqlHelpers
|
||||
{
|
||||
|
||||
public static readonly string connectionString = "Server=blv-cloud-db.mysql.rds.aliyuncs.com;Database=face;Uid=blv_rcu;Pwd=fnadiaJDIJ7546;charset=utf8;port=3307;";
|
||||
|
||||
public MysqlHelpers() { }
|
||||
#region ExecuteNonQuery
|
||||
//执行SQL语句,返回影响的记录数
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteNonQuery(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteNonQuery(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//执行多条SQL语句,实现数据库事务。
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">多条SQL语句</param>
|
||||
public static bool ExecuteNoQueryTran(List<String> SQLStringList)
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
cmd.Connection = conn;
|
||||
MySqlTransaction tx = conn.BeginTransaction();
|
||||
cmd.Transaction = tx;
|
||||
try
|
||||
{
|
||||
for (int n = 0; n < SQLStringList.Count; n++)
|
||||
{
|
||||
string strsql = SQLStringList[n];
|
||||
if (strsql.Trim().Length > 1)
|
||||
{
|
||||
cmd.CommandText = strsql;
|
||||
PrepareCommand(cmd, conn, tx, strsql, null);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
cmd.ExecuteNonQuery();
|
||||
tx.Commit();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
tx.Rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region ExecuteScalar
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object ExecuteScalar(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object ExecuteScalar(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
object obj = cmd.ExecuteScalar();
|
||||
cmd.Parameters.Clear();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region ExecuteReader
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>MySqlDataReader</returns>
|
||||
public static MySqlDataReader ExecuteReader(string strSQL)
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection(connectionString);
|
||||
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return myReader;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>MySqlDataReader</returns>
|
||||
public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection(connectionString);
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
cmd.Parameters.Clear();
|
||||
return myReader;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
// finally
|
||||
// {
|
||||
// cmd.Dispose();
|
||||
// connection.Close();
|
||||
// }
|
||||
}
|
||||
#endregion
|
||||
#region ExecuteDataTable
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataTable
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable ExecuteDataTable(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds.Tables[0];
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable ExecuteDataTable(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
da.Fill(ds, "ds");
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds.Tables[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
//获取起始页码和结束页码
|
||||
public static DataTable ExecuteDataTable(string cmdText, int startResord, int maxRecord)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataAdapter command = new MySqlDataAdapter(cmdText, connection);
|
||||
command.Fill(ds, startResord, maxRecord, "ds");
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds.Tables[0];
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 获取分页数据 在不用存储过程情况下
|
||||
/// </summary>
|
||||
/// <param name="recordCount">总记录条数</param>
|
||||
/// <param name="selectList">选择的列逗号隔开,支持top num</param>
|
||||
/// <param name="tableName">表名字</param>
|
||||
/// <param name="whereStr">条件字符 必须前加 and</param>
|
||||
/// <param name="orderExpression">排序 例如 ID</param>
|
||||
/// <param name="pageIdex">当前索引页</param>
|
||||
/// <param name="pageSize">每页记录数</param>
|
||||
/// <returns></returns>
|
||||
public static DataTable getPager(out int recordCount, string selectList, string tableName, string whereStr, string orderExpression, int pageIdex, int pageSize)
|
||||
{
|
||||
int rows = 0;
|
||||
DataTable dt = new DataTable();
|
||||
MatchCollection matchs = Regex.Matches(selectList, @"top\s+\d{1,}", RegexOptions.IgnoreCase);//含有top
|
||||
string sqlStr = sqlStr = string.Format("select {0} from {1} where 1=1 {2}", selectList, tableName, whereStr);
|
||||
if (!string.IsNullOrEmpty(orderExpression)) { sqlStr += string.Format(" Order by {0}", orderExpression); }
|
||||
if (matchs.Count > 0) //含有top的时候
|
||||
{
|
||||
DataTable dtTemp = ExecuteDataTable(sqlStr);
|
||||
rows = dtTemp.Rows.Count;
|
||||
}
|
||||
else //不含有top的时候
|
||||
{
|
||||
string sqlCount = string.Format("select count(*) from {0} where 1=1 {1} ", tableName, whereStr);
|
||||
//获取行数
|
||||
object obj = ExecuteScalar(sqlCount);
|
||||
if (obj != null)
|
||||
{
|
||||
rows = Convert.ToInt32(obj);
|
||||
}
|
||||
}
|
||||
dt = ExecuteDataTable(sqlStr, (pageIdex - 1) * pageSize, pageSize);
|
||||
recordCount = rows;
|
||||
return dt;
|
||||
}
|
||||
#region 创建command
|
||||
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = cmdText;
|
||||
if (trans != null)
|
||||
cmd.Transaction = trans;
|
||||
cmd.CommandType = CommandType.Text;//cmdType;
|
||||
if (cmdParms != null)
|
||||
{
|
||||
foreach (MySqlParameter parameter in cmdParms)
|
||||
{
|
||||
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||||
(parameter.Value == null))
|
||||
{
|
||||
parameter.Value = DBNull.Value;
|
||||
}
|
||||
cmd.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
4234
Face.Services/DBUtility/Common/SqlHelper.cs
Normal file
4234
Face.Services/DBUtility/Common/SqlHelper.cs
Normal file
File diff suppressed because it is too large
Load Diff
654
Face.Services/DBUtility/Common/SqlOperationsData.cs
Normal file
654
Face.Services/DBUtility/Common/SqlOperationsData.cs
Normal file
@@ -0,0 +1,654 @@
|
||||
using Face.Domain.Application;
|
||||
using Face.Domain.Entities;
|
||||
using Face.Services.Cache;
|
||||
using Face.Services.Enums;
|
||||
using Face.Services.Manager;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WebSocketToolsConsole;
|
||||
|
||||
namespace Face.Services.DBUtility.Common
|
||||
{
|
||||
public static class SqlOperationsData
|
||||
{
|
||||
static SqlDependency dependency;
|
||||
|
||||
/// <summary>
|
||||
/// 更新人脸机状态
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DataSet facestate()
|
||||
{
|
||||
string sql = "select Facestate,snnumber from DeviceManage";
|
||||
SqlDataAdapter sqldata = new SqlDataAdapter(sql, MysqlHelpers.connectionString);
|
||||
DataSet data = new DataSet();
|
||||
sqldata.Fill(data);
|
||||
return data;
|
||||
}
|
||||
/// <summary>
|
||||
/// 数据库更新发生变化
|
||||
/// </summary>
|
||||
public static void Monitor()
|
||||
{
|
||||
//Start和Stop方法
|
||||
SqlDependency.Start(MysqlHelpers.connectionString);
|
||||
Update(MysqlHelpers.connectionString);
|
||||
}
|
||||
private static void Update(string conn)
|
||||
{
|
||||
using (
|
||||
SqlConnection connection =
|
||||
new SqlConnection(conn))
|
||||
{
|
||||
//此处 要注意 不能使用* 表名要加[dbo] 否则会出现一直调用执行 OnChange
|
||||
string sql = "select Status from [dbo].[DeviceManage]";
|
||||
|
||||
using (SqlCommand command = new SqlCommand(sql, connection))
|
||||
{
|
||||
connection.Open();
|
||||
command.CommandType = CommandType.Text;
|
||||
dependency = new SqlDependency(command);
|
||||
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
|
||||
//必须要执行一下command
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
//update insert delete都会进入
|
||||
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
|
||||
{
|
||||
CacheHelp.ClearFaceList();
|
||||
//using (var db=new )
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加顾客信息
|
||||
/// </summary>
|
||||
public static int addClient(Lodger ci)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("select LodgerId from Lodger where IDNumber='" + ci.IDNumber + "'");
|
||||
object id = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (id != null && Convert.ToInt64(id) > 0)
|
||||
{
|
||||
sql = string.Format("update Lodger set phonenumber='{0}' where LodgerId='{1}'", ci.phonenumber, id);
|
||||
int ss = MysqlHelpers.ExecuteNonQuery(sql);
|
||||
return ss;
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = "insert into Lodger(LodgerNmae, IDNumber, Sex, CheckInDate, Sourcedian,phonenumber,remark)VALUES(@LodgerNmae,@IDNumber,@Sex,@CheckInDate,@Sourcedian,@phonenumber,@remark)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[7] {
|
||||
new MySqlParameter("@LodgerNmae",ci.LodgerNmae),
|
||||
new MySqlParameter("@IDNumber", ci.IDNumber),
|
||||
new MySqlParameter("@Sex",ci.Sex),
|
||||
new MySqlParameter("@CheckInDate",DateTime.Now),
|
||||
new MySqlParameter("@Sourcedian",ci.Sourcedian),
|
||||
new MySqlParameter("@remark", ci.remark),
|
||||
new MySqlParameter("@phonenumber", ci.phonenumber)};
|
||||
return MysqlHelpers.ExecuteNonQuery(sql, sqlParams);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加入住信息
|
||||
/// </summary>
|
||||
public static int addcheckininfo(CheckInInfo co)
|
||||
{
|
||||
string sql = "insert into CheckInInfo(Name, IdNumber, CreationTime, InfoSource, HotelCode, Roomid,CheckTime,checkOutTime,picture,identitys)VALUES(@Name,@IdNumber,@CreationTime,@InfoSource,@HotelCode,@Roomid,@CheckTime,@checkOutTime,@picture,@identitys)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[10] {
|
||||
new MySqlParameter("@Name",co.Name),
|
||||
new MySqlParameter("@IdNumber", co.IdNumber),
|
||||
new MySqlParameter("@CreationTime",co.CreationTime),
|
||||
new MySqlParameter("@InfoSource",co.InfoSource),
|
||||
new MySqlParameter("@HotelCode",co.HotelCode),
|
||||
new MySqlParameter("@Roomid", co.Roomid),
|
||||
new MySqlParameter("@CheckTime", co.CheckTime),
|
||||
new MySqlParameter("@checkOutTime", co.checkOutTime),
|
||||
new MySqlParameter("@picture", co.picture),
|
||||
new MySqlParameter("@identitys", co.identitys) };
|
||||
return MysqlHelpers.ExecuteNonQuery(sql,sqlParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接口,查看是否有此人脸机
|
||||
/// </summary>
|
||||
/// <param name="hotelname"></param>
|
||||
/// <param name="roomname"></param>
|
||||
/// <returns></returns>
|
||||
public static string selectFace(string hotelname, string roomname)
|
||||
{
|
||||
string consequence = "";
|
||||
try
|
||||
{
|
||||
string sql = "select SerialNo from DeviceManage where HotelCode='" + hotelname + "' and RoomId='" + roomname + "'";
|
||||
object id = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (id != null)
|
||||
{
|
||||
consequence = id.ToString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
}
|
||||
return consequence;
|
||||
}
|
||||
/// <summary>
|
||||
/// 为房间绑定人脸机
|
||||
/// </summary>
|
||||
public static int reviseRommFace(string hotel, int romm, string faceNo)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE DeviceManage SET HotelCode='{0}',RoomId='{1}',bindingStatus=1,bindingDate=CURRENT_DATE() where SerialNo='{2}' ", hotel, romm, faceNo);
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为酒店绑定人脸机
|
||||
/// </summary>
|
||||
/// <param name="hotel"></param>
|
||||
/// <param name="romm"></param>
|
||||
/// <param name="faceNo"></param>
|
||||
/// <returns></returns>
|
||||
public static int reviseHotelFace(string hotel, string faceNo)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE DeviceManage SET HotelCode='{0}',bindingStatus=1,bindingDate=CURRENT_DATE() where SerialNo='{1}' ", hotel, faceNo);
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 记录绑定/解绑操作
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int bindingState(FaceBinding fb)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = "insert into FaceBinding(SerialNo, Operator, OperatorType, bindingDate, HotelCode,RoomId)VALUES(@SerialNo,@Operator,@OperatorType,@bindingDate,@HotelCode,@RoomId)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[6] {
|
||||
new MySqlParameter("@SerialNo", fb.SerialNo),
|
||||
new MySqlParameter("@Operator",fb.Operator),
|
||||
new MySqlParameter("@OperatorType",fb.OperatorType),
|
||||
new MySqlParameter("@bindingDate",fb.bindingDate),
|
||||
new MySqlParameter("@HotelCode", fb.HotelCode),
|
||||
new MySqlParameter("@RoomId", fb.RoomId)};
|
||||
return MysqlHelpers.ExecuteNonQuery(sql,sqlParams);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 登录
|
||||
/// </summary>
|
||||
/// <param name="accountentity"></param>
|
||||
//public static Accountentity LoginAccount(string uid)
|
||||
//{
|
||||
// List<Accountentity> listay = new List<Accountentity>();
|
||||
// string sql = "select * from AuthorityDB.dbo.UserInfo where Uid='" + uid + "'";
|
||||
// SqlDataReader dr = DbHelperMySQL.ExecuteReader(sql);
|
||||
// Accountentity ay = new Accountentity();
|
||||
// while (dr.Read())
|
||||
// {
|
||||
// ay.Uid = dr["Uid"].ToString();
|
||||
// ay.CreateTime = dr["CreateTime"].ToDateTime();
|
||||
// }
|
||||
// return ay;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 查询酒店权限
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static List<string> permission(string name)
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
string sql = "select distinct HotelId from AuthorityDB.dbo.UserAuthoes where UserId in(select Id from AuthorityDB.dbo.UserInfo where Uid='" + name + "')";
|
||||
DbHelperMySQL.ExecuteReader(sql, dr => {
|
||||
while (dr.Read())
|
||||
{
|
||||
list.Add(dr["HotelId"].ToString());
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
/// <summary>
|
||||
/// 登录验证
|
||||
/// </summary>
|
||||
/// <param name="accountentity"></param>
|
||||
//public static Accountentity LoginAccount(string uid, string pwd)
|
||||
//{
|
||||
// List<Accountentity> listay = new List<Accountentity>();
|
||||
// string sql = "select * from AuthorityDB.dbo.UserInfo where Uid='" + uid + "'and Pwd='" + pwd + "'";
|
||||
// SqlDataReader dr = DbHelperMySQL.ExecuteReader(sql);
|
||||
// Accountentity ay = new Accountentity();
|
||||
// while (dr.Read())
|
||||
// {
|
||||
// ay.Uid = dr["Uid"].ToString();
|
||||
// ay.CreateTime = dr["CreateTime"].ToDateTime();
|
||||
// }
|
||||
// return ay;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 解绑房间
|
||||
/// </summary>
|
||||
public static int unbundleoperateRoom(string sn)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = "UPDATE DeviceManage SET RoomId=null,bindingStatus=0 WHERE SerialNo='" + sn + "'";
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解绑酒店
|
||||
/// </summary>
|
||||
public static int unbundleoperatehotel(string sn)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = "UPDATE DeviceManage SET RoomId=null,bindingStatus=0,HotelCode=null WHERE SerialNo='" + sn + "'";
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
///// <summary>
|
||||
///// 通过酒店id查询房间列表
|
||||
///// </summary>
|
||||
//public static Hosts Roominfo(string hotelname)
|
||||
//{
|
||||
// string sql = "select * from Hosts where HotelID=" + hotelname + "";
|
||||
// SqlDataReader dr = DbHelperMySQL.ExecuteReader(sql);
|
||||
// Hosts ay = new Hosts();
|
||||
// while (dr.Read())
|
||||
// {
|
||||
// ay.HotelID = int.Parse(dr["HotelID"].ToString());
|
||||
// ay.RoomNumber = dr["RoomNumber"].ToString();
|
||||
// ay.Status = int.Parse(dr["Status"].ToString());
|
||||
// }
|
||||
// return ay;
|
||||
//}
|
||||
/// <summary>
|
||||
/// 添加入住信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int additionCheck(CheckInInfo cf)
|
||||
{
|
||||
try
|
||||
{
|
||||
string date = cf.CreationTime.Value.ToString("yyyy-MM-dd");
|
||||
string sql = string.Format("select * from CheckInInfo where HotelCode=" + cf.HotelCode + " and IdNumber='" + cf.IdNumber + "'and Roomid="+cf.Roomid+"");
|
||||
object id = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (id != null && Convert.ToInt64(id) > 0)
|
||||
{
|
||||
string dateout = cf.checkOutTime.Value.ToString("yyyy-MM-dd");
|
||||
sql = string.Format("UPDATE CheckInInfo SET HotelCode = '{0}', RoomId = '{1}',checkOutTime='{2}',picture='{3}' where id={4}", cf.HotelCode, cf.Roomid,dateout,cf.picture, id);
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
string sql2 = "insert into CheckInInfo (Name,CreationTime,IdNumber,InfoSource,HotelCode,Roomid,CheckTime,checkOutTime,picture) values(@Name,@CreationTime,@IdNumber,@InfoSource,@HotelCode,@Roomid,@CheckTime,@checkOutTime,@picture)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[9] {
|
||||
new MySqlParameter("@Name",cf.Name),
|
||||
new MySqlParameter("@CreationTime",cf.CreationTime),
|
||||
new MySqlParameter("@IdNumber",cf.IdNumber),
|
||||
new MySqlParameter("@InfoSource",cf.InfoSource),
|
||||
new MySqlParameter("@HotelCode",cf.HotelCode),
|
||||
new MySqlParameter("@Roomid",cf.Roomid),
|
||||
new MySqlParameter("@CheckTime",cf.CheckTime),
|
||||
new MySqlParameter("@checkOutTime",cf.checkOutTime.Value.ToString("yyyy/MM/dd")),
|
||||
new MySqlParameter("@picture",cf.picture) };
|
||||
return MysqlHelpers.ExecuteNonQuery(sql2, sqlParams);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 绑定/解绑操作记录
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int bindingoperation(FaceBinding fb)
|
||||
{
|
||||
string sql = "insert into FaceBinding (SerialNo,Operator,OperatorType,bindingDate,HotelCode,RoomId) values(@SerialNo,@Operator,@OperatorType,@bindingDate,@HotelCode,@RoomId)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[6] {
|
||||
new MySqlParameter("@SerialNo",fb.SerialNo),
|
||||
new MySqlParameter("@Operator",fb.Operator),
|
||||
new MySqlParameter("@OperatorType",fb.OperatorType),
|
||||
new MySqlParameter("@bindingDate",fb.bindingDate),
|
||||
new MySqlParameter("@HotelCode",fb.HotelCode),
|
||||
new MySqlParameter("@RoomId",fb.RoomId) };
|
||||
return MysqlHelpers.ExecuteNonQuery(sql, sqlParams);
|
||||
}
|
||||
/// <summary>
|
||||
///// 获取人脸机地理位置
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
public static List<string> facelocation()
|
||||
{
|
||||
string sql = "SELECT faceAddress from DeviceManage WHERE HotelCode='' GROUP BY faceAddress ";
|
||||
List<string> location = new List<string>();
|
||||
|
||||
DbHelperMySQL.ExecuteReader(sql, dr => {
|
||||
|
||||
while (dr.Read())
|
||||
{
|
||||
location.Add(dr["faceAddress"].ToString());
|
||||
}
|
||||
});
|
||||
return location;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 保存pms数据
|
||||
/// </summary>
|
||||
public static int preserve(string info, string hotelid, string room,string facesn,int issueresult,int pmstype)
|
||||
{
|
||||
string sql = "insert into pmsInterface values(CURRENT_DATE(),@pmsContent,@hotelid,@room,@faceSN,@issueresult,@pmstype)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[6] {
|
||||
new MySqlParameter("@pmsContent",info),
|
||||
new MySqlParameter("@hotelid",hotelid),
|
||||
new MySqlParameter("@room",room),
|
||||
new MySqlParameter("@faceSN",facesn),
|
||||
new MySqlParameter("@issueresult",issueresult),
|
||||
new MySqlParameter("@pmstype",pmstype)};
|
||||
return MysqlHelpers.ExecuteNonQuery(sql, sqlParams);
|
||||
}
|
||||
/// <summary>
|
||||
/// 操作日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int writeLog(ULog ul)
|
||||
{
|
||||
string sql = "insert into ulog values(@Uname,@Creationtime,@type,@faceSN,@hotelCode,@roomid)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[6] {
|
||||
new MySqlParameter("@Uname",ul.Uname),
|
||||
new MySqlParameter("@Creationtime",DateTime.Now),
|
||||
new MySqlParameter("@type",ul.operatetype),
|
||||
new MySqlParameter("@faceSN",ul.faceSN),
|
||||
new MySqlParameter("@hotelCode",ul.hotelcode),
|
||||
new MySqlParameter("@roomid",ul.roomid)};
|
||||
return MysqlHelpers.ExecuteNonQuery(sql, sqlParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日常日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int writeDailyLog(Dailyoperation dp)
|
||||
{
|
||||
string sql = "insert into Dailyoperation values(@Uname,CURRENT_DATE(),@type,@operatedata,@hotelCode)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[4] {
|
||||
new MySqlParameter("@Uname",dp.Uname),
|
||||
new MySqlParameter("@type",dp.operatetype),
|
||||
new MySqlParameter("@operatedata",dp.operatedata),
|
||||
new MySqlParameter("@hotelCode",dp.hotelCode)};
|
||||
return MysqlHelpers.ExecuteNonQuery(sql, sqlParams);
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据酒店code获取酒店id
|
||||
/// </summary>
|
||||
/// <param name="hotelid"></param>
|
||||
/// <returns></returns>
|
||||
public static string geihotelid(string hotelid)
|
||||
{
|
||||
string sql = string.Format("select Id from [AuthorityDB].[dbo].Hotels where Code={0}", hotelid);
|
||||
object roomid = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (roomid == null)
|
||||
{
|
||||
roomid = "";
|
||||
}
|
||||
return roomid.ToString();
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据房间号获取房间id
|
||||
/// </summary>
|
||||
/// <param name="hotelid"></param>
|
||||
/// <param name="roomname"></param>
|
||||
/// <returns></returns>
|
||||
public static string geiroomid(string hotelid, string roomname)
|
||||
{
|
||||
string sql = string.Format("select Id ,RoomStatusID,HotelID,[RoomNumber],[Status] = convert(int,[Status]),[Desc] =[remark],[CreateTime]=[registerdate] from BLW.CRICS.[dbo].tb_Hosts where HotelID={0} and RoomNumber='{1}'", hotelid, roomname);
|
||||
object roomid = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (roomid == null)
|
||||
{
|
||||
roomid = "";
|
||||
}
|
||||
return roomid.ToString();
|
||||
}
|
||||
/// <summary>
|
||||
/// 通过酒店id和房间id获取入住人的信息
|
||||
/// </summary>
|
||||
public static List<string> getCheck(string code,string room)
|
||||
{
|
||||
List<string> idnumber = new List<string>();
|
||||
string sql = string.Format("select IdNumber from CheckInInfo where checkOutTime = '2000-01-01' and HotelCode={0} and Roomid={1}", code,room);
|
||||
DbHelperMySQL.ExecuteReader(sql,dr=> {
|
||||
|
||||
while (dr.Read())
|
||||
{
|
||||
idnumber.Add(dr["IdNumber"].ToString());
|
||||
}
|
||||
});
|
||||
|
||||
return idnumber;
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加退房时间
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <param name="hotel"></param>
|
||||
/// <param name="room"></param>
|
||||
/// <returns></returns>
|
||||
public static int reviseDate(string date,string hotel,string room)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE CheckInInfo SET checkOutTime ='{0}' WHERE HotelCode={1} and Roomid={2} and checkOutTime='2000-01-01'", date, hotel, room);
|
||||
int sd= MysqlHelpers.ExecuteNonQuery(sql);
|
||||
return sd;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 冻结测试人员
|
||||
/// </summary>
|
||||
/// <param name="tiaojian"></param>
|
||||
/// <returns></returns>
|
||||
public static int amendtestUser(string tiaojian)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE TestUser SET state =0 WHERE id={0}",tiaojian);
|
||||
int sd = MysqlHelpers.ExecuteNonQuery(sql);
|
||||
return sd;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 解冻测试人员
|
||||
/// </summary>
|
||||
/// <param name="tiaojian"></param>
|
||||
/// <returns></returns>
|
||||
public static int amendtestUser1(string tiaojian)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE TestUser SET state =1 WHERE id={0}", tiaojian);
|
||||
int sd = MysqlHelpers.ExecuteNonQuery(sql);
|
||||
return sd;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 人脸机维修状态
|
||||
/// </summary>
|
||||
public static int maintainStaet(transferFace tf)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("select id from transferFace where faceSN='"+tf.faceSN+"'");
|
||||
object id = MysqlHelpers.ExecuteScalar(sql);
|
||||
if (id != null && Convert.ToInt64(id) > 0)
|
||||
{
|
||||
sql = string.Format("UPDATE transferFace SET infoid = '{0}', faultState = '{1}',creationTime='{2}' where faceSN={3}", tf.infoid, tf.faultState, tf.creationTime, id);
|
||||
return MysqlHelpers.ExecuteNonQuery(sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
string sql2 = "insert into transferFace (faceSN,infoid,faultState,creationTime) values(@faceSN,@infoid,@faultState,@creationTime)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[4] {
|
||||
new MySqlParameter("@faceSN",tf.faceSN),
|
||||
new MySqlParameter("@infoid",tf.infoid),
|
||||
new MySqlParameter("@faultState",tf.faultState),
|
||||
new MySqlParameter("@creationTime",tf.creationTime) };
|
||||
return MysqlHelpers.ExecuteNonQuery(sql2, sqlParams);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加pms日志
|
||||
/// </summary>
|
||||
/// <param name="log"></param>
|
||||
/// <returns></returns>
|
||||
public static int revise(pmsLog log)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql2 = "insert into pmsLog (pmsid,step,app,Creationtime,message,Data) values(@pmsid,@step,@app,@Creationtime,@message,@Data)";
|
||||
MySqlParameter[] sqlParams = new MySqlParameter[6] {
|
||||
new MySqlParameter("@pmsid",log.pmsid),
|
||||
new MySqlParameter("@step",log.step),
|
||||
new MySqlParameter("@app",log.app),
|
||||
new MySqlParameter("@Creationtime",log.Creationtime),
|
||||
new MySqlParameter("@message",log.message),
|
||||
new MySqlParameter("@Data",log.Data) };
|
||||
return MysqlHelpers.ExecuteNonQuery(sql2, sqlParams);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加pms酒店房间信息
|
||||
/// </summary>
|
||||
/// <param name="pms"></param>
|
||||
/// <returns></returns>
|
||||
public static int revisepms(pmsInterface pms)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = string.Format("UPDATE pmsInterface SET faceSN ='{0}',hotelid={1},room={2},messageid='{3}' WHERE pmsId={4}",pms.faceSN,pms.hotelid,pms.room,pms.messageid,pms.pmsId);
|
||||
int sd = MysqlHelpers.ExecuteNonQuery(sql);
|
||||
return sd;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.WriteExceptionLog(ex);
|
||||
return 0;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
///// <summary>
|
||||
///// 获取所有的房间
|
||||
///// </summary>
|
||||
//public static List<TBL_ROOM_BASIC_INFO> Roomdata()
|
||||
//{
|
||||
// List<TBL_ROOM_BASIC_INFO> Room = new List<TBL_ROOM_BASIC_INFO>();
|
||||
|
||||
// string sql = "select * from tbl_room_basic_info";
|
||||
// MySqlDataReader dr = MysqlHelper.ExecuteReader(sql);
|
||||
// while (dr.Read())
|
||||
// {
|
||||
// TBL_ROOM_BASIC_INFO log = new TBL_ROOM_BASIC_INFO();
|
||||
// log.ROOM_NUMBER = dr["ROOM_NUMBER"].ToString();
|
||||
// log.RoomStatusID = int.Parse(dr["RoomStatusID"].ToString());
|
||||
// log.HOTEL_OLD_ID = int.Parse(dr["HOTEL_OLD_ID"].ToString());
|
||||
// log.ROOM_OLD_ID = int.Parse(dr["ROOM_OLD_ID"].ToString());
|
||||
// Room.Add(log);
|
||||
// }
|
||||
// return Room;
|
||||
//}
|
||||
}
|
||||
}
|
||||
478
Face.Services/DBUtility/Custom/DALHelperCustom.cs
Normal file
478
Face.Services/DBUtility/Custom/DALHelperCustom.cs
Normal file
@@ -0,0 +1,478 @@
|
||||
using Face.Domain.Entities;
|
||||
using Fasce.Services.DBUtility.Custom;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WebSocketToolsConsole;
|
||||
|
||||
namespace Face.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<SqlParameter> parameters = new List<SqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + ",");
|
||||
}
|
||||
strSql.Remove(strSql.Length - 1, 1);//移除最后一个逗号
|
||||
strSql.Append(" ) values (");
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append("@" + pi.Name + ",");
|
||||
parameters.Add(new SqlParameter("@" + 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<SqlParameter> parameters = new List<SqlParameter>();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
|
||||
if (!pi.CanWrite) continue;
|
||||
strSql.Append(pi.Name + "=@" + pi.Name + ",");
|
||||
parameters.Add(new SqlParameter("@" + pi.Name, pi.GetValue(entity)));
|
||||
}
|
||||
strSql.Remove(strSql.Length - 1, 1);//移除最后一个逗号
|
||||
strSql.Append(" where " + strField + "=@strValue");
|
||||
strSql.Replace("Operatorid=@Operatorid,", "");
|
||||
System.Type keyType = keyValue.GetType();
|
||||
switch (keyType.Name)
|
||||
{
|
||||
case "Int32":
|
||||
SqlParameter strValue = new SqlParameter("@strValue", SqlDbType.Int)
|
||||
{
|
||||
Value = keyValue
|
||||
};
|
||||
parameters.Add(strValue);
|
||||
break;
|
||||
default:
|
||||
parameters.Add(new SqlParameter("@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);
|
||||
}
|
||||
SqlParameter[] parameters = {
|
||||
new SqlParameter("@strValue",strValue)
|
||||
};
|
||||
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 ");
|
||||
SqlParameter[] parameters = {
|
||||
new SqlParameter("@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;");
|
||||
SqlParameter[] parameters = {
|
||||
new SqlParameter("@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 null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
|
||||
public List<pmsLog> StoredprocedureList(List<int> ints)
|
||||
{
|
||||
return DbHelperMySqlCustom.Storedprocedure(ints);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 将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":
|
||||
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":
|
||||
bool fatr=false;
|
||||
if (value.ToString()== "1")
|
||||
{
|
||||
fatr = true;
|
||||
}
|
||||
pi.SetValue(t, fatr, 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
1294
Face.Services/DBUtility/Custom/DbHelperMySqlCustom.cs
Normal file
1294
Face.Services/DBUtility/Custom/DbHelperMySqlCustom.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user