|
|
|
|
#region CopyRight
|
|
|
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
|
* Project:健康体检信息管理系统(PEIS)
|
|
|
|
|
* Author:张剑峰
|
|
|
|
|
* CLR Version:4.0.30319.42000
|
|
|
|
|
* CreateTime:2023-05-01 14:43:04
|
|
|
|
|
* Version:v2.0
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
*
|
|
|
|
|
* History:
|
|
|
|
|
*
|
|
|
|
|
*****************************************************************
|
|
|
|
|
* Copyright @ 云南新八达科技有限公司 2023 All rights reserved
|
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
|
|
#endregion CopyRight
|
|
|
|
|
|
|
|
|
|
using PEIS.Utils;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using DcmToPng.Helper;
|
|
|
|
|
|
|
|
|
|
namespace DicomTool.Utils
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* 操作数据库的方法
|
|
|
|
|
* 约定:
|
|
|
|
|
* 1)对象属性必须有public string TableName
|
|
|
|
|
* 2)属性名与字段名一样。
|
|
|
|
|
* 3)主键属性需标记自定义特性KeyFlagAttribute,表示此属性是数据表中的主键
|
|
|
|
|
* 4)数据表中每个表必须设有主键
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class DAOHelp
|
|
|
|
|
{
|
|
|
|
|
#region 属性
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据库连接字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
#endregion 属性
|
|
|
|
|
|
|
|
|
|
#region 数据库增、删、改、查
|
|
|
|
|
|
|
|
|
|
public static DataTable GetDataBySql(string sql)
|
|
|
|
|
{
|
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
|
DataTable dt = new DataTable();
|
|
|
|
|
using (SqlConnection conn = new SqlConnection(Constant.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
conn.Open();
|
|
|
|
|
SqlCommand cmd = new SqlCommand(sql, conn);
|
|
|
|
|
cmd.CommandTimeout = Convert.ToInt32(60);
|
|
|
|
|
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
|
|
|
|
|
|
|
|
|
|
foreach (DataColumn column in dt.Columns)
|
|
|
|
|
{
|
|
|
|
|
if (column.DataType == typeof(string))
|
|
|
|
|
{
|
|
|
|
|
column.DefaultValue = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adapter.Fill(dt);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("GetDataBySQL异常:" + sql + "," + e.Message);
|
|
|
|
|
Console.WriteLine("数据库连接失败,请检查网络,或联系管理员!" + e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 数据库连接池
|
|
|
|
|
//DBConnectionSingletion pool = DBConnectionSingletion.Instance;
|
|
|
|
|
//DBConnectionSingletion.ConnectionString = connectionString;
|
|
|
|
|
//SqlConnection conn = pool.BorrowDBConnection();
|
|
|
|
|
//SqlCommand cmd = new SqlCommand(sql, conn);
|
|
|
|
|
//SqlDataAdapter adapter = new SqlDataAdapter(cmd);
|
|
|
|
|
//adapter.Fill(dt);
|
|
|
|
|
//pool.ReturnDBConnection(conn);
|
|
|
|
|
|
|
|
|
|
//Global.CostTime("conn.Open", StartTime);
|
|
|
|
|
|
|
|
|
|
return dt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行非查询SQL
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int ExecuteSql(String sql, Boolean startTrans = false)
|
|
|
|
|
{
|
|
|
|
|
//sql = sql.ToUpper();
|
|
|
|
|
Debug.WriteLine("执行SQL=>" + sql);
|
|
|
|
|
int n = 0;//影响的行数
|
|
|
|
|
DateTime StartTime = DateTime.Now;
|
|
|
|
|
DataTable dt = new DataTable();
|
|
|
|
|
using (SqlConnection conn = new SqlConnection(Constant.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
if (startTrans)
|
|
|
|
|
{
|
|
|
|
|
SqlTransaction trans = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
conn.Open();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("数据库连接失败,请检查网络,或联系管理员!" + e.Message);
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
trans = conn.BeginTransaction();
|
|
|
|
|
SqlCommand cmd = new SqlCommand(sql, conn);
|
|
|
|
|
cmd.CommandTimeout = Convert.ToInt32(60);
|
|
|
|
|
n = cmd.ExecuteNonQuery();
|
|
|
|
|
trans.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
trans.Rollback();
|
|
|
|
|
Debug.WriteLine(sql);
|
|
|
|
|
Console.WriteLine("SQL执行错误!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
conn.Open();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("数据库连接失败,请检查网络,或联系管理员!" + e.Message);
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SqlCommand cmd = new SqlCommand(sql, conn);
|
|
|
|
|
n = cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(sql);
|
|
|
|
|
Console.WriteLine("SQL执行错误!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<T> GetDataBySQL<T>(String sql) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("执行SQL=>" + sql);
|
|
|
|
|
Type type = typeof(T);
|
|
|
|
|
List<T> list = new List<T>();
|
|
|
|
|
DataTable dt = GetDataBySql(sql.ToUpper());
|
|
|
|
|
|
|
|
|
|
foreach (DataRow row in dt.Rows)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo[] pArray = type.GetProperties();
|
|
|
|
|
T entity = new T();
|
|
|
|
|
foreach (PropertyInfo p in pArray)
|
|
|
|
|
{
|
|
|
|
|
if (dt.Columns.IndexOf(p.Name) == -1)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (p.Name == "TableName")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (row[p.Name] is String)
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, row[p.Name], null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (row[p.Name] is Int32)
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, Convert.ToInt32(row[p.Name]), null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (row[p.Name] is Int64)
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, Convert.ToInt64(row[p.Name]), null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (row[p.Name] is DateTime)
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, Convert.ToDateTime(row[p.Name]), null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (row[p.Name] is byte[])
|
|
|
|
|
{
|
|
|
|
|
//Image image = Global.BytesToImage((byte[])row[p.Name]);
|
|
|
|
|
p.SetValue(entity, row[p.Name], null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (row[p.Name] is DBNull)
|
|
|
|
|
{
|
|
|
|
|
if (p.PropertyType.Name == "Int64" || p.PropertyType.Name == "Int32")
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, 0, null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (p.PropertyType.Name == "Decimal")
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, Convert.ToDecimal(0), null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (p.PropertyType.Name == "Boolean")
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, false, null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (p.PropertyType.Name == "Image" || p.PropertyType.Name == "Byte[]")
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, null, null);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
p.SetValue(entity, null, null);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//object[] attrs = p.GetCustomAttributes(false);
|
|
|
|
|
//if (attrs.Count() > 0)
|
|
|
|
|
//{
|
|
|
|
|
// if (attrs[0].GetType().Name.Equals("RefFlagAttribute"))
|
|
|
|
|
// {
|
|
|
|
|
// if (attrs.Count() == 1)
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
p.SetValue(entity, row[p.Name], null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion 数据库增、删、改、查
|
|
|
|
|
}
|
|
|
|
|
}
|