test notes:
IDAL/DALBase.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace IDAL
{
public class DALBase
{
private IPersistBroker _persistBroker;
public DALBase()
{
this._persistBroker = PersistBrokerManager.PersistBroker("");
}
///
/// 数据库连接
///
protected IPersistBroker PersistBroker
{
get
{
return _persistBroker;
}
}
///
/// 检查记录是否存在
///
///
public virtual bool IsExist(DomainObject obj )
{
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return false;
}
string _field = string.Empty;
string wstr = string.Empty;
foreach (PropertyInfo item in properties)
{
string name = item.Name.Replace("_PK", "");
object value = item.GetValue(obj, null);
if (item.Name.IndexOf("_PK") > -1)
{
wstr += " " + name.Replace("_PK", "") + "='" + (value == null ? "" : value.ToString()) + "' and";
}
_field += name + ",";
}
_field = _field.TrimEnd(new char[] { ',' });
wstr = wstr.Remove(wstr.Length - 3, 3);
string tbname = "TBL" + obj.GetType().Name;
string sqlText = string.Format("SELECT {0} FROM {1} WHERE {2} ", _field, tbname, wstr);
DataSet ds = null;
ds = this.PersistBroker.Query(sqlText);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
return true;
}
return false;
}
public bool AddDomainObject(DomainObject obj)
{
try
{
try
{
DBDateTime dbDateTime = GetNowDBDateTime();
DateTime workDateTime = FormatHelper.ToDateTime(dbDateTime.DBDate, dbDateTime.DBTime);
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo item in properties)
{
string name = item.Name.ToUpper();
if (name.Contains("MDATE"))
{
item.SetValue(obj, FormatHelper.TODateInt(workDateTime), null);
}
if (name.Contains("MTIME"))
{
item.SetValue(obj, FormatHelper.TOTimeInt(workDateTime), null);
}
}
}
catch
{
throw new Exception();
}
this.Insert(obj);
return true;
}
catch (Exception ex)
{
PersistBroker.RollbackTransaction();
ExceptionManager.Raise(obj.GetType(), "$Error_Add_Domain_Object", ex);
return false;
}
finally
{
}
}
public bool AddDomainObject(List
{
PersistBroker.BeginTransaction();
try
{
foreach (DomainObject obj in objs)
{
this.AddDomainObject(obj);
}
PersistBroker.CommitTransaction();
return true;
}
catch (Exception ex)
{
PersistBroker.RollbackTransaction();
ExceptionManager.Raise(objs[0].GetType(), "$Error_Add_Domain_Object", ex);
return false;
}
finally
{
}
}
private void Insert(object domainObject)
{
PropertyInfo[] properties = domainObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0)
{
return;
}
string _field = string.Empty;
string _value = string.Empty;
foreach (PropertyInfo item in properties)
{
string name = item.Name.Replace("_PK", "");
object value = item.GetValue(domainObject, null);
_field += name + ",";
_value += "'" + (value == null ? "" : value.ToString()) + "',";
}
_field = _field.TrimEnd(new char[] { ',' });
_value = _value.TrimEnd(new char[] { ',' });
string tbname = "TBL" + domainObject.GetType().Name;
string sqlText = string.Format("INSERT INTO {0}({1}) VALUES ({2})", tbname, _field, _value);
this.PersistBroker.Execute(sqlText);
}
protected static List
{
List
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
DataTable table = ds.Tables[0];
objectList = new List
foreach (DataRow dr in table.Rows)
{
objectList.Add(FillDomainObject(dr));
}
}
return objectList;
}
protected static T[] FillDomainObject(DataTable table)
{
T[] domainObjects = new T[table.Rows.Count];
int num1 = 0;
foreach (DataRow dr in table.Rows)
{
domainObjects[num1] = FillDomainObject(dr);
num1 = num1 + 1;
}
return domainObjects;
}
protected static T FillDomainObject(DataRow dataRow)
{
object obj = typeof(T).Assembly.CreateInstance(typeof(T).FullName);
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo item in properties)
{
string name = item.Name.ToUpper();
foreach (DataColumn column in dataRow.Table.Columns)
{
if (name.Contains(column.ColumnName.ToUpper()))
{
Type type1 = item.PropertyType;
var value = dataRow[column.ColumnName];
if (type1 == typeof(int))
{
if ((value is System.DBNull)|| (value.ToString()=="null"))
{
value = 0;
}
item.SetValue(obj, int.Parse(value.ToString()), null);
}
if (type1 == typeof(long))
{
if ((value is System.DBNull) || (value.ToString() == "null"))
{
value = 0;
}
item.SetValue(obj, Int64.Parse(value.ToString()), null);
}
if (type1 == typeof(double))
{
if ((value is System.DBNull) || (value.ToString() == "null"))
{
value = 0;
}
item.SetValue(obj, Double.Parse(value.ToString()), null);
}
if (type1 == typeof(float))
{
if ((value is System.DBNull) || (value.ToString() == "null"))
{
value = 0;
}
item.SetValue(obj, Single.Parse(value.ToString()), null);
}
if (type1 == typeof(decimal))
{
if ((value is System.DBNull) || (value.ToString() == "null"))
{
value = 0;
}
item.SetValue(obj, Getdecimal(value), null);
}
if (type1 == typeof(bool))
{
item.SetValue(obj, value.ToString() == "Y", null);
}
if (type1 == typeof(string))
{
if ((value is System.DBNull) || (value.ToString() == "null"))
{
value = string.Empty;
}
item.SetValue(obj, value.ToString(), null);
}
if (type1 == typeof(DateTime))
{
DateTime result;
DateTime.TryParse(value.ToString(), out result);
item.SetValue(obj, result, null);
}
}
}
}
return (T)obj;
}
}
}
-----------------------------------------------------------------------------------------------
IBLL/BLLBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IBLL
{
public class BLLBase
{
///
/// 通过反射获得程序集中某类型的实例
///
///
///
private T _dalInstance;
protected T DalInstance
{
get
{
if (_dalInstance == null)
{
_dalInstance= (T)CreateTypeInstance(typeof(T));
return _dalInstance;
}
else
{
return _dalInstance;
}
}
}
protected object CreateTypeInstance(Type type)
{
return type.Assembly.CreateInstance(type.FullName);
}
}
}
------------------------------------------------------------------------------------------------
DAL/DalORG.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class DalORG : DALBase
{
public List
{
string sql = " SELECT * FROM TBLORG ";
List
***
return orgList;
}
}
}
---------------------------------------------------------------------------------------
BLL/BllORG.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class BllORG : BLLBase
{
public ORG GetSingleORG(string orgID)
{
ORG org = this.DalInstance.CustomSearch(new object[] { orgID });
return org;
}
public List
{
List
return orgList;
}
}
}