📄 pubfuncs.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
{
public class PubFuncs
{
public static readonly string ConnectionStr = "";
private static Hashtable _HashKeyName = new Hashtable();
private static Hashtable _HashForeignKeyName = new Hashtable();
private static Hashtable _HashTablename = new Hashtable();
private static Hashtable _HashObjectType = new Hashtable();
/// <summary>
/// 返回一个连接
/// </summary>
/// <returns></returns>
public static SqlConnection GetConnection()
{
SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr);
return conn;
}
/// <summary>
/// 返回对象的Select语句,在读取对象的时候使用
/// </summary>
/// <param name="ModelObject"></param>
/// <returns></returns>
public static string GetReadSQL(object ModelObject, ref List<SqlParameter> Params)
{
string strSelectSQL = "SELECT {0} FROM {1} WHERE {2};";
string strTablename = "";
string strWhere = "";
StringBuilder sbFields = new StringBuilder();
//取数据库表名
strTablename = PubFuncs.GetTableName(ModelObject);
PropertyInfo[] props = ModelObject.GetType().GetProperties();
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
object[] CustomAttributes;
int i = 0;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomAttributes.Length > 0)
{
FieldAttr = CustomAttributes[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr is MiniORMAttribute.DataFieldAttribute)
{
if (i > 0) sbFields.Append(",");
sbFields.Append(FieldAttr.FieldName);
i++;
//对于标识字段,不做处理
if (FieldAttr.IsIdentity)
{
//构造 " WHERE ID = @ID "这样的语句
strWhere += (((strWhere.Length == 0) ? " " : " AND ") + FieldAttr.FieldName + " = @" + FieldAttr.FieldName);
//采用在GetInsertSQL中,顺便进行Param的初始化,这样避免两次对ModelObject进行反射操作
Params.Add(new SqlParameter("@" + FieldAttr.FieldName, prop.GetValue(ModelObject, null)));
}
}
}
}
if (strWhere == "")
{
//如果没有设置Indentity字段的话,那么设置不读取信息
strWhere = " 1=2 ";
}
if (sbFields.ToString().Trim() != "")
{
string[] Args = new string[] { sbFields.ToString(), strTablename, strWhere };
string strReturn = string.Format(strSelectSQL, Args);
return strReturn;
}
else
{
throw new Exception(ModelObject.ToString() + "构造Select语句失败,字段字符串为空。");
}
}
/// <summary>
/// 返回Insert语句:格式:Insert into Tablename (Field1,Field2,...Fieldn) Values(@Field1,@Field2,...@Fieldn)
/// </summary>
/// <param name="ModelObject"></param>
/// <param name="Params"></param>
/// <returns></returns>
public static string GetInsertSQL(object ModelObject, ref List<SqlParameter> Params)
{
string strTablename = "";
string strInsertSQL = "DECLARE @IDENTITY_ID INT,@ERR_CODE INT;INSERT INTO {0}({1}) VALUES({2});SELECT @IDENTITY_ID = @@IDENTITY,@ERR_CODE=@@ERROR;SELECT @IDENTITY_ID, @ERR_CODE";
System.Text.StringBuilder strFields = new System.Text.StringBuilder();
System.Text.StringBuilder strValues = new System.Text.StringBuilder();
int i = 0;
//取数据库表名
strTablename = GetTableName(ModelObject);
PropertyInfo[] props = ModelObject.GetType().GetProperties();
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
object[] CustomAttributes;
i = 0;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomAttributes.Length > 0)
{
FieldAttr = CustomAttributes[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr != null)
{
if (FieldAttr is MiniORMAttribute.DataFieldAttribute)
{
//对于标识字段,不做处理
if (!FieldAttr.IsIdentity)
{
if (i > 0)
{
strFields.Append(",");
strValues.Append(",");
}
//只处理值类型字段,对于引用类型或者其他类型的变量,将作为Model对象来另外处理
strFields.Append(FieldAttr.FieldName);
strValues.Append("@" + FieldAttr.FieldName);
i++;
}
//采用在GetInsertSQL中,顺便进行Param的初始化,这样避免两次对ModelObject进行反射操作
Params.Add(new SqlParameter("@" + FieldAttr.FieldName, prop.GetValue(ModelObject, null)));
}
}
}
}
if (strFields.ToString().Trim() != "" && strValues.ToString().Trim() != "")
{
string[] Args = new string[] { strTablename, strFields.ToString(), strValues.ToString() };
string strReturn = string.Format(strInsertSQL, Args);
return strReturn;
}
else
{
throw new Exception(ModelObject.ToString() + "构造Insert语句失败,字段字符串为空。");
}
}
/// <summary>
/// 返回Update语句,如果ModelObject.PrimaryKey不存在,那么将返回InsertSQL
/// </summary>
/// <param name="ModelObject"></param>
/// <returns></returns>
public static string GetUpdateSQL(object ModelObject, ref List<SqlParameter> Params)
{
string strUpdateSQL = "UPDATE {0} SET {1} WHERE {2}";
string strTablename = "";
string strWHERE = "";
StringBuilder strSET = new StringBuilder();
int i = 0;
//取数据库表名
strTablename = GetTableName(ModelObject);
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
PropertyInfo[] props = ModelObject.GetType().GetProperties();
object[] CustomAttributes;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomAttributes.Length > 0)
{
FieldAttr = CustomAttributes[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr != null)
{
if (FieldAttr.IsIdentity)
{
object keyvalue = prop.GetValue(ModelObject, null);
//如果要修改的记录主键为0(对于int类型)或者为空(对于char型),那么执行插入操作
if (Convert.ToString(keyvalue) == "0" || Convert.ToString(keyvalue) == "")
{
//重新清空Parameter,避免在InsertSQL中发生重复
Params.Clear();
return GetInsertSQL(ModelObject, ref Params);
}
strWHERE = FieldAttr.FieldName + " = @" + FieldAttr.FieldName;
}
else
{
if (i > 0) strSET.Append(",");
strSET.Append(FieldAttr.FieldName + " = @" + FieldAttr.FieldName);
i++;
}
//采用在GetInsertSQL中,顺便进行Param的初始化,这样避免两次对ModelObject进行反射操作
Params.Add(new SqlParameter("@" + FieldAttr.FieldName, prop.GetValue(ModelObject, null)));
}
}
}
if (strSET.Length > 0)
{
return string.Format(strUpdateSQL, new string[] { strTablename, strSET.ToString(), strWHERE });
}
return "";
}
/// <summary>
/// 返回指定程序集,指定命名空间,指定类名的类型
/// </summary>
/// <param name="assemblyname">程序集名</param>
/// <param name="namespacename">命名空间</param>
/// <param name="classname">类名</param>
/// <returns></returns>
public static Type GetObjectType(string assemblyname, string namespacename, string classname)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -