📄 cmyaaron工具.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using tw;
using System.Reflection;
/// </姓名>Aaron
/// </時間>03-04-1651
/// </內容>增加C查詢類別
public class CMyAaron工具
{
public MyTrans TransferSqltoObject;
public Object[] queryByObject(Object p_course, String tableName, params String[] p_strs)
{
PropertyInfo[] l_properties = p_course.GetType().GetProperties();
String l_strQueryString = "SELECT * FROM " + tableName + " Where 1=1";
foreach (PropertyInfo p in l_properties)
{
l_strQueryString = createQueryStringByLike(p_course, p_strs, l_strQueryString, p);
}
return TransferSqltoObject(l_strQueryString);
}
private String createQueryStringByLike(Object p_course, String[] p_strs, String l_strQueryString, PropertyInfo p)
{
foreach (String s in p_strs)
{
if (s.GetType().Equals(typeof(String)))
{
if (s.Equals(p.Name))
{
l_strQueryString += " AND " + s + " LIKE'%" + p.GetValue(p_course, null).ToString() + "%'";
}
}
else
l_strQueryString += " AND " + s + "LIKE %" + p.GetValue(p_course, null).ToString() + "";
}
return l_strQueryString;
}
public string 自動新增Sql字串(object p_object, string p_tableName)
{
PropertyInfo[] l_properties = p_object.GetType().GetProperties();
string l_str = "INSERT INTO " + p_tableName + "(";
for (int i = 0; i < l_properties.Length; i++)
{
if (l_properties[i].Name.Equals("TeacherID") || l_properties[i].Name.Equals("IID") || l_properties[i].Name.Equals("CID"))
{
continue;
}
if (i == l_properties.Length - 1)
{
l_str += "" + l_properties[i].Name + "";
}
else
{
l_str += "" + l_properties[i].Name + ",";
}
}
l_str +=") VALUES (";
for (int i = 0; i < l_properties.Length; i++)
{
if (l_properties[i].Name.Equals("TeacherID") || l_properties[i].Name.Equals("IID") || l_properties[i].Name.Equals("CID"))
{
continue;
}
if (i == l_properties.Length - 1)
{
l_str += "'" + l_properties[i].GetValue(p_object, null).ToString() + "'";
}
else
{
l_str += "'" + l_properties[i].GetValue(p_object, null).ToString() + "',";
}
}
l_str +=")";
return l_str;
}
public string 自動修改sql字串(object p_object, string p_tableName)
{
PropertyInfo[] l_propertys = p_object.GetType().GetProperties();
string l_str = "UPDATE " + p_tableName + " SET ";
for (int i = 0; i < l_propertys.Length; i++)
{
if (l_propertys[i].Name.Equals("TeacherID") || l_propertys[i].Name.Equals("IID") || l_propertys[i].Name.Equals("CID"))
{
continue;
}
if (i == l_propertys.Length - 1)
{
l_str += l_propertys[i].Name + "='" + l_propertys[i].GetValue(p_object, null).ToString() + "'";
}
else
{
l_str += l_propertys[i].Name + "='" + l_propertys[i].GetValue(p_object, null).ToString() + "',";
}
}
return l_str;
}
public Object[] checkByObject(Object p_course, String tableName, params String[] p_strs)
{
PropertyInfo[] l_properties = p_course.GetType().GetProperties();
String l_strQueryString = "SELECT * FROM " + tableName + " Where 1=1";
foreach (PropertyInfo p in l_properties)
{
l_strQueryString = createQueryStringByCheck(p_course, p_strs, l_strQueryString, p);
}
return TransferSqltoObject(l_strQueryString);
}
private String createQueryStringByCheck(Object p_course, String[] p_strs, String l_strQueryString, PropertyInfo p)
{
foreach (String s in p_strs)
{
if (s.GetType().Equals(typeof(String)))
{
if (s.Equals(p.Name))
{
l_strQueryString += " AND " + s + "='" + p.GetValue(p_course, null).ToString() + "'";
}
}
else
l_strQueryString += " AND " + s + "= " + p.GetValue(p_course, null).ToString() + "";
}
return l_strQueryString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -