📄 readermapper.cs
字号:
using System;
using System.Collections;
using System.Reflection;
using System.Data;
namespace iSun.SPL.ORM
{
/// <summary>
/// 通过数据表实体类生成相应insert update delete 语句
/// 实现简单ORM
/// 作者:江怀玉
/// 2005-02-16
/// </summary>
public class ReaderMapper
{
public ReaderMapper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public ReaderMapper(object obj)
{
this._obj = obj;
}
private object _obj;
public object Obj
{
get{return _obj;}
set{_obj = value;}
}
private string _tablename;
public string TableName
{
get{return _tablename;}
set{_tablename = value;}
}
/// <summary>
/// 标识种子,当实体类对应的属性的值为空时,新增记录,否则就是修改记录。
/// </summary>
private string _guid;
public string Guid
{
get{return _guid;}
set{_guid = value;}
}
/// <summary>
/// 新增记录
/// </summary>
/// <returns>返回插入的记录数,-1为失败</returns>
public int Add()
{
Hashtable ht = new Hashtable();
try
{
ht = this.ReaderAutoMapper();
return new iSun.DbDAL.DbSql().ExecuteSql(this.BuildInsertComm(ht));
}
catch(System.Exception e)
{
return -1;
}
}
/// <summary>
/// 更新记录
/// </summary>
/// <returns>更新的记录数,-1为失败.</returns>
public int Update()
{
Hashtable ht = new Hashtable();
try
{
ht = this.ReaderAutoMapper();
return new iSun.DbDAL.DbSql().ExecuteSql(this.BuildUpdateComm(ht));
}
catch
{
return -1;
}
}
/// <summary>
/// 通过唯一值更新<see cref="Update()">重构Update()</see>
/// </summary>
/// <param name="isGuid">ture or false</param>
/// <returns>更新的记录数,-1为失败.</returns>
public int Update(bool isGuid)
{
return this.Update();
}
// public int Update(IList[] conditions)
// {
// IEnumerator _iec = conditions.GetEnumerator();
// while (_iec.MoveNext())
// {
// _iec.Current
// }
// }
/// <summary>
/// 用于删除该记录
/// </summary>
/// <returns></returns>
public int Delete()
{
string _sqlcomm="delete from ["+this.TableName+"] where "+this.Guid + " = '"+this.GetPropertyValue(this.Guid)+"'";
try
{
return new iSun.DbDAL.DbSql().ExecuteSql(_sqlcomm);
}
catch
{
return -1;
}
}
/// <summary>
/// 生成数据操作的SQL
/// </summary>
/// <param name="ht">存储实体类信息的HashTable</param>
/// <returns></returns>
public string BuildInsertComm(Hashtable ht)
{
Hashtable _ht = ht;
if (this.Guid!="")
{
_ht.Remove(this.Guid);
}
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string _fieldlist = string.Empty;
string _valuelist = string.Empty;
int i = 0;
try
{
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
_fieldlist += myEnumerator.Key.ToString();
_valuelist +=this.FieldConvert(myEnumerator.Value);
}
else
{
_fieldlist += "," + myEnumerator.Key.ToString();
_valuelist +="," + this.FieldConvert(myEnumerator.Value);
}
i ++;
}
return "insert into "+this.TableName+" ("+_fieldlist+") values ("+_valuelist+")";
}
catch(System.Exception e)
{
// write err to log
throw;
}
}
/// <summary>
/// 生成数据操作的SQL
/// </summary>
/// <param name="ht">存储实体类信息的HashTable</param>
/// <returns>Insert into Sql</returns>
public string BuildUpdateComm(Hashtable ht)
{
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string _Setlist = string.Empty;;
try
{
while ( myEnumerator.MoveNext() )
{
_Setlist += myEnumerator.Key.ToString()+ " = " ;
_Setlist += this.FieldConvert(myEnumerator.Value)+",";
}
_Setlist = _Setlist.Remove(_Setlist.Length-1,1);
return "Update " + this.TableName + " Set "+ _Setlist +" where "+this.Guid + " = '"+this.GetPropertyValue(this.Guid)+"'";
}
catch(System.Exception e)
{
// write err to log
throw e;
}
}
private void FieldConvert(ref string Value,ref string DataType,object obj)
{
Value = null;
DataType = "C";
if (obj == null)
{
Value = null;
DataType = "C";
}
if (obj is System.Int32)
{
Value = ((Int32)obj).ToString();
DataType = "N";
}
if (obj is System.DateTime)
{
Value = ((DateTime)obj).ToString("yyyy-MM-dd");
DataType = "D";
}
if (obj is System.String)
{
Value = ((String)obj).ToString();
DataType = "C";
}
if (obj is System.Decimal)
{
Value = ((Decimal)obj).ToString();
DataType = "N";
}
if (obj is System.Double)
{
Value = ((Double)obj).ToString();
DataType = "N";
}
}
private string FieldConvert(object obj)
{
string Value = string.Empty;
string DataType = string.Empty;
this.FieldConvert(ref Value,ref DataType,obj);
return "";
//return FieldConvert(Value,DataType);
}
public Hashtable ReaderAutoMapper()
{
Hashtable ht = new Hashtable();
try
{
// 利用反射获类的属性名称和属性值
Type t = this.Obj.GetType();
PropertyInfo[] Mypropertyinfos = t.GetProperties();
foreach(PropertyInfo pi in Mypropertyinfos)
{
//
Object Value = t.InvokeMember(pi.Name,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty , null, this.Obj , null);
ht.Add(pi.Name,Value);
// attr = pi.GetType().GetCustomAttributes(true);
}
foreach (Attribute attr in t.GetCustomAttributes(true))
{
iSun.Entitys.TableAttribute tableattribute = attr as iSun.Entitys.TableAttribute;
if (null != tableattribute)
{
this._tablename = tableattribute.TableName;
this._guid = tableattribute.Guid;
}
}
}
catch(System.Exception e)
{
throw e;
}
return ht;
}
/// <summary>
/// 动态设置实体类的属性
/// </summary>
/// <param name="name"></param>
/// <param name="objValue"></param>
public void SetPropertyValue(string name, object objValue)
{
object[] objArray1 = new object[1];
Type type1 = this.Obj.GetType().GetProperty(name).PropertyType;
objArray1[0] = Convert.ChangeType(objValue, type1);
try
{
this.Obj.GetType().InvokeMember(name, BindingFlags.SetProperty, null, this.Obj, objArray1);
}
catch (Exception exception1)
{
throw exception1;
}
}
/// <summary>
/// 获取属性的值
/// </summary>
/// <param name="name">属性名</param>
/// <returns>属性值</returns>
public object GetPropertyValue(string name)
{
object obj1 = null;
try
{
obj1 = this.Obj.GetType().InvokeMember(name, BindingFlags.GetProperty, null, this.Obj, null);
}
catch (Exception exception1)
{
throw exception1;
}
return obj1;
}
/*------------------------------------------------------
// 实例应用:
//
// Employee em = new iSunXoft.Model.Employee.Employee();
// em.Name = "jianghuaiyu";
// em.Relation ="ddd" ;
// new Common.ReaderMapper(em).Save();
//
//
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -