📄 data.cs
字号:
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
return o_Instance;
}
}
}
return Ilst;
}
// ==============================================================
// ===========================内部调用函数============================
// ==============================================================
/// <summary>
/// 获得删除Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回删除sql语句</returns>
public static string GetDelSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="Delete "+Table;
}
else
{
str_Sql="Delete "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 获得插入Sql语句
/// </summary>
/// <param name="TableName">数据库表名</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回插入Sql语句</returns>
public static string GetInsertSqlbyHt(string TableName, Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string before="";
string behide="";
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
before="("+myEnumerator.Key;
}
else if (i+1==ht_Count)
{
before=before+","+myEnumerator.Key+")";
}
else
{
before=before+","+myEnumerator.Key;
}
i=i+1;
}
behide=" Values"+before.Replace(",",",@").Replace("(","(@");
str_Sql="Insert into "+TableName+before+behide;
return str_Sql;
}
/// <summary>
/// 获得记录数sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetPageListCountSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (str_Ht!="")
{
str_Sql="Select Count(*) From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select Count(*) From "+Table;
}
}
else
{
str_Sql="Select Count(*) From "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 通过传递哈希表参数,获得更新Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetUpdateSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="update "+Table+" set "+str_Sql;
}
else
{
str_Sql="update "+Table+" set "+str_Sql+" where "+ht_Where;
}
str_Sql=str_Sql.Replace("set ,","set ").Replace("update ,","update ");
return str_Sql;
}
/// <summary>
/// 获得IList分页Sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="orderby">排序</param>
/// <param name="ht">表示层传递过来的条件字段参数</param>
/// <param name="class_Name">实体类名</param>
/// <returns></returns>
public static string GetPageListSqlbyHt(string Table,string ht_Where,string orderby,Hashtable ht,String class_Name)
{
string str_Sql="";
// 选择类型只能实现 Select * from table where a=@a and b=@b效果
// where 后面优先权,当ht_Where不为空或者不为null,条件应该是ht_Where参数,否则,用ht做循环
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
if (k==0)
{
str_Sql=myPropInfo.Name.ToString();
}
else
{
str_Sql=str_Sql+","+myPropInfo.Name.ToString();
}
}
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (orderby=="" || orderby==null)
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
else
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht+" order by "+orderby;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
}
else // 用ht_Where做条件
{
if (orderby=="" || orderby==null)
{
str_Sql="Select "+str_Sql+" From "+Table+" Where "+ht_Where;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+ht_Where+" order by "+orderby;
}
}
return str_Sql;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -