1.创建一个SQL生成帮助类分别写出以下增删改三个公共方法 数据插入方法
public static int Save(object obj)
{
int n = 0;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
StringBuilder sqlend = new StringBuilder();
PropertyInfo[] pro = obj.GetType().GetProperties();
List<PropertyInfo> idlist = GetIdProperty(pro);
string table = FindPropertyInfoValue(obj, "TableName").ToString();
string sqltext = string.Empty;
sql.Append("INSERT INTO " + table + "(");
sqlend.Append(" VALUES (");
foreach (PropertyInfo item in pro)
{
if (item.Name == "TableName")
{
continue;
}
else
{
string columnValue = item.GetValue(obj, null) + "";
if (string.IsNullOrEmpty(columnValue))
{
continue;
}
if (item.PropertyType == typeof(DateTime))
{
DateTime dt;
DateTime.TryParse(columnValue, out dt);
if (dt <= SqlDateTime.MinValue.Value)
continue;
}
sql.Append(" " + item.Name + ",");
sqlend.Append(" '" + columnValue + "',");
}
}
string start = sql.ToString();
start = start.Substring(0, start.Length - 1) + ")";
string end = sqlend.ToString();
end = end.Substring(0, end.Length - 1) + ")";
sqltext = start + end;
using (SqlCommand cmd = new SqlCommand(sqltext, conn))
{
n = cmd.ExecuteNonQuery();
}
}
return n;
}
数据删除方法
public static int DeleteById(object pdata)
{
int n = 0;
PropertyInfo[] pro = pdata.GetType().GetProperties();
List<PropertyInfo> idlist = GetIdProperty(pro);
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
StringBuilder sqlend = new StringBuilder();
string table = FindPropertyInfoValue(pdata, "TableName").ToString();
string sqltext = string.Empty;
sql.AppendFormat("delete from {0} where", table);
foreach (PropertyInfo item in idlist)
{
sql.Append(" " + item.Name + " = '" + item.GetValue(pdata, null) + "'");
}
sqltext = sql.ToString();
using (SqlCommand cmd = new SqlCommand(sqltext, conn))
{
n = cmd.ExecuteNonQuery();
}
}
return n;
}
数据修改方法
public static int Update(object obj)
{
int n = 0;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
StringBuilder sqlend = new StringBuilder();
PropertyInfo[] pro = obj.GetType().GetProperties();
List<PropertyInfo> idlist = GetIdProperty(pro);
string table = FindPropertyInfoValue(obj, "TableName").ToString();
string sqltext = string.Empty;
sql.Append("UPDATE " + table + " set");
sqlend.Append("WHERE");
foreach (PropertyInfo item in pro)
{
if (item.Name == "TableName")
{
continue;
}
else
{
sql.Append(" " + item.Name + "= '" + item.GetValue(obj, null) + "',"+"");
}
}
foreach (PropertyInfo item in idlist)
{
sqlend.Append(" " + item.Name + "= '" + item.GetValue(obj, null) + "'"+" and ");
}
string start = sql.ToString();
start = start.Substring(0, start.Length - 1) + " ";
string end = sqlend.ToString();
end = end.Substring(0, end.Length - 5) + " ";
sqltext = start + end;
using (SqlCommand cmd = new SqlCommand(sqltext, conn))
{
n = cmd.ExecuteNonQuery();
}
}
return n;
}
2.编写 一个数据对象抽象类 生成的数据表永远是动态的所以这里定义成抽象类,便于实体数据类继承和具体实现
public abstract class ObjectData
{
public abstract string TableName
{
get;set;
}
public virtual bool Delete()
{
if (DAL.GenratesSQLHelp.DeleteById(this) > 0)
{
return true;
}
else
{
return false;
}
}
public virtual bool Save()
{
if (DAL.GenratesSQLHelp.Save(this) > 0)
{
return true;
}
else
{
return false;
}
}
public virtual bool Update()
{
if (DAL.GenratesSQLHelp.Update(this) > 0)
{
return true;
}
else
{
return false;
}
}
}
3.定义一个数据实体类继承数据对象抽象类
public class Empinfo:BLL.ObjectData
{
[KeyFlagAttribute(true)]
public int empid { get; set; }
public string empName { get; set; }
public DateTime empBirth { get; set; }
public string empsex { get; set; }
public string emptel { get; set; }
public int deptid { get; set; }
public override bool Save()
{
return base.Save();
}
public override bool Delete()
{
return base.Delete();
}
public override bool Update()
{
return base.Update();
}
public override T Get<T>(T pobj)
{
return base.Get<T>(pobj);
}
public override string TableName
{
get;set;
}
}
4 .主函数方法内调用执行
static void Main(string[] args)
{
Empinfo emp = new Empinfo();
string table = "Empinfo";
/emp.TableName = table;
emp.empid = 7;
emp.empName = "张峰";
emp.empBirth = DateTime.Now;
emp.empsex = "男";
emp.emptel = "12345678";
emp.deptid = 2;
emp.Save();
Empinfo emp = new Empinfo();
string table = "Empinfo";
emp.TableName = table;
emp.empid = 7;
emp.Delete();
Empinfo emp = new Empinfo();
string table = "Empinfo";
emp.TableName = table;
emp.empName = "张三丰";
emp.empsex = "男";
emp.emptel = "12345678910";
emp.deptid = 1;
emp.empBirth = Convert.ToDateTime("2004-03-05");
emp.deptid = 1;
emp.empid = 6;
emp.Update();
}
|