📄 ormwriter.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Transactions;
namespace MiniORM
{
/// <summary>
/// ORM写入器,向数据库中写入记录
/// </summary>
public class OrmWriter
{
public object Write(object ModelObject)
{
using (SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr))
{
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
ModelObject = Write(trans, ModelObject);
trans.Commit();
return ModelObject;
}
}
/// <summary>
/// 向数据库中插入INS.Model中的对象
/// </summary>
/// <param name="ModelObject">记录Model</param>
/// <returns>记录Model</returns>
public object Write(SqlTransaction trans, object ModelObject)
{
CheckValidate(ModelObject);
//新记录ID
int iNewID = 0;
List<SqlParameter> Params = new List<SqlParameter>();
//插入SQL,并且把SqlParameters的初始化也放在里面,避免两次进行ModelObject的反射操作
string strSQL = PubFuncs.GetInsertSQL(ModelObject, ref Params);
//// 构造Params数组
//SqlParameter[] Params = GetParamsWithValue(ModelObject);
SqlCommand cmd = new SqlCommand();
foreach (SqlParameter Param in Params)
{
cmd.Parameters.Add(Param);
}
cmd.Connection = trans.Connection;
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSQL;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
if (rdr.GetInt32(1) != 0)
throw new ApplicationException("插入新记录" + ModelObject.ToString() + "出错,SQL错误号:" + rdr.GetInt32(1)); ;
cmd.Parameters.Clear();
iNewID = rdr.GetInt32(0); //返回ID
//设置对象的Key值
PubFuncs.SetKeyValue(ModelObject, iNewID);
}
#region 保存子对象
PropertyInfo[] props = ModelObject.GetType().GetProperties();
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
object[] CustomAttributes;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomAttributes.Length > 0)
{
SubDataAttr = CustomAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
switch (SubDataAttr.FieldType)
{
case MiniORMAttribute.SubDataObjectFieldType.Object:
{
object objModel = prop.GetValue(ModelObject, null);
//设置objModel的Foreignkey为当前新记录的ID
PubFuncs.SetForeignkeyValue(objModel, iNewID);
//插入对象
Write(trans, objModel);
}
break;
//case MiniORMAttribute.SubDataObjectFieldType.MyHashTable:
// {
// System.Collections.ICollection colValues = ((INS.Model.MyHashTable)prop.GetValue(ModelObject, null)).Values;
// foreach (object obj in colValues)
// {
// //设置objModel的Foreignkey为当前新记录的ID
// PubFuncs.SetForeignkeyValue(obj, iNewID);
// //插入对象
// Write(trans, obj);
// }
// }
// break;
case MiniORMAttribute.SubDataObjectFieldType.HashTable:
{
System.Collections.ICollection colValues = ((System.Collections.Hashtable)prop.GetValue(ModelObject, null)).Values;
foreach (object obj in colValues)
{
//设置objModel的Foreignkey为当前新记录的ID
PubFuncs.SetForeignkeyValue(obj, iNewID);
//插入对象
Write(trans, obj);
}
}
break;
case MiniORMAttribute.SubDataObjectFieldType.List:
{
System.Collections.IList list = (System.Collections.IList)prop.GetValue(ModelObject, null);
foreach (object obj in list)
{
//设置objModel的Foreignkey为当前新记录的ID
PubFuncs.SetForeignkeyValue(obj, iNewID);
//插入对象
Write(trans, obj);
}
}
break;
case MiniORMAttribute.SubDataObjectFieldType.ArrayList:
{
System.Collections.ArrayList list = (System.Collections.ArrayList)prop.GetValue(ModelObject, null);
foreach (object obj in list)
{
//设置objModel的Foreignkey为当前新记录的ID
PubFuncs.SetForeignkeyValue(obj, iNewID);
//插入对象
Write(trans, obj);
}
}
break;
}
}
}
}
#endregion
return ModelObject;
}
public void CheckValidate(object ModelObject)
{
PropertyInfo[] infos = ModelObject.GetType().GetProperties();
object[] CustomerAttributes = null;
object[] CustomerAttributes1 = null;
//SELECT ID FROM TableName WHERE @A='' OR @B=''
string strSQL = "SELECT ID FROM {0} WHERE {1}";
string strTablename = PubFuncs.GetTableName(ModelObject);
string strWOA = "";
string strWHERE = "";
string strFieldMessage = "";
foreach (PropertyInfo info in infos)
{
CustomerAttributes = info.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldNotDoubleAttribute), false);
CustomerAttributes1 = info.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomerAttributes != null && CustomerAttributes1 != null)
{
for (int i = 0; i < CustomerAttributes.Length; i++)
{
strWHERE += strWOA + ((MiniORMAttribute.DataFieldAttribute)CustomerAttributes1[0]).FieldName + "='" + info.GetValue(ModelObject, null) + "'";
strFieldMessage += ((MiniORMAttribute.DataFieldAttribute)CustomerAttributes1[0]).FieldName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -