//定义 特性类:

 

 

public class MyAttribute : Attribute//自定义注解类判断是否是主键
{
public bool PrimaryKey = false;
public string Type = null;
}

 

//完成个实体类:

public class Student
{
private int _Id;
[My(PrimaryKey =true,Type ="自动增长")]
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _name;
[My(PrimaryKey =false,Type ="名字")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _Age;
[My(PrimaryKey = false, Type = "年龄")]
public int Age
{
get { return _Age; }
set { _Age = value; }
}
private string _Remark;
[My(PrimaryKey =false,Type ="优点")]
public string Remark
{
get { return _Remark; }
set { _Remark = value; }
}

public Student(int id,string name,int age,string remark)
{
this._Id = id;
this._name = name;
this._Age = age;
this._Remark = remark;
}

 

}

 

//--执行方法:

 

class Program
{
static void Main(string[] args)
{
Student stu = new Student(1, "张三", 21, "很多的");//实体类
// Insert(stu);//添加
// Update(stu);//修改
SInsert<Student>(stu);
Console.ReadKey();
}
public static void SInsert<T>(T obj) where T : class
{
string Filed = string.Empty;//字段值初始化
string Values = string.Empty;//属性值初始化

Type type = obj.GetType();
//获取类名
string TableName = type.Name;
//获取所有公有属性
PropertyInfo[] info = type.GetProperties();

foreach (PropertyInfo var in info)
{
//取得属性的特性标签,false表示不获取因为继承而得到的标签
object[] attr = var.GetCustomAttributes(false);
if (attr.Length > 0)
{
//从注解数组中取第一个注解(一个属性可以包含多个注解)
MyAttribute myattr = attr[0] as MyAttribute;
if (myattr.PrimaryKey == true)
{
continue;
}
}
Filed += var.Name + ",";
Values += "'" + var.GetValue(obj, null) + "',";
}
Filed = Filed.Substring(0, Filed.Length - 1);
Values = Values.Substring(0, Values.Length - 1);
string sql = "insert into {0}({1}) values({2})";
sql = string.Format(sql, TableName, Filed, Values);
Console.WriteLine(sql);
}
public static void Insert(object obj)
{
PropertyInfo[] moderarr = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
string val = string.Empty;//预先定义返回值,全局变量为空
string filed = string.Empty;//预先定义属性值,全局变量为空
Type type = obj.GetType();
string className = type.Name;//根据 [Type] 属性完成对实体类对象的获取
foreach (PropertyInfo item in moderarr)
{
string PropertyValue = item.GetValue(obj, null).ToString();//预定义一个属性值接收所返回的属性值
Object[] attr = item.GetCustomAttributes(false);//返回自定义特性数组
if (attr.Length > 0)
{
MyAttribute myattr = attr[0] as MyAttribute;
if (myattr.PrimaryKey == true)//利用C#自定义特性 [MyAttrbute] 判断对象是否是主键
{
continue;
}
}
if (item.PropertyType.Name.StartsWith("Int32") || item.PropertyType.Name.StartsWith("Decimal"))//利用属性类型匹配对应元素信息
{
PropertyValue = item.GetValue(obj, null).ToString();//返回指定对象的属性值
val += PropertyValue + ",";
filed += item.Name + ",";
}
if (item.PropertyType.Name.StartsWith("String") || item.PropertyType.Name.StartsWith("Boolean") || item.PropertyType.Name.StartsWith("DateTime"))
{
PropertyValue = item.GetValue(obj, null).ToString();
val += "'" + PropertyValue + "',";
filed += item.Name + ",";
}
}
val = val.TrimEnd(',');//完成对返回值 Values( ? ) 的获取
filed = filed.TrimEnd(',');//完成对属性值insert 表名 ( ? ) 的获取
string sql = "insert into {0} ({1}) values({2})";
sql = string.Format(sql, className, filed, val);//具体的实现语句
Console.WriteLine(sql);
}

public static void Update(object obj)
{
PropertyInfo[] modelarr = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
string val = string.Empty;
string val2 = string.Empty;
string PropertyName = string.Empty;
string PropertyValue = string.Empty;
Type type = obj.GetType();
string className = type.Name;//根据 [Type] 属性完成对实体类对象的获取
foreach (PropertyInfo item in modelarr)
{
PropertyValue = string.Empty;
PropertyName = item.Name;
if (PropertyName.ToLower().Contains("id"))
{
PropertyValue = item.GetValue(obj, null).ToString();
if (item.PropertyType.Name.StartsWith("Int32"))
{
val2 = PropertyName + " = " + PropertyValue;
}
else
{
val2 = PropertyName + " = '" + PropertyValue + "'";
}
continue;
}
if (item.PropertyType.Name.StartsWith("Int32") || item.PropertyType.Name.StartsWith("Decimal"))
{
PropertyValue = item.GetValue(obj, null).ToString();
val += PropertyName + " = " + PropertyValue + ",";
}
if (item.PropertyType.Name.StartsWith("String") || item.PropertyType.Name.StartsWith("Boolean") || item.PropertyType.Name.StartsWith("DateTime"))
{
PropertyValue = item.GetValue(obj, null).ToString();
val += PropertyName + " = '" + PropertyValue + "',";
}
}
val = val.TrimEnd(',');
string sql = "update {0} set {1} where {2}";
sql = string.Format(sql, className, val, val2);//具体的实现语句
Console.WriteLine(sql);


}

public static List<T> Drop<T>(string ProcName)where T :new()
{
//数据库连接字符串
List<T> list = new List<T>();
string strc = System.Configuration.ConfigurationManager.AppSettings["Conn"].ToString();
SqlConnection conn = new SqlConnection(strc);
if (conn.State!=ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(ProcName, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rearer = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Type type = typeof(T);
T entity = new T();
while(rearer.Read())
{
foreach (var item in type.GetProperties())
{
item.SetValue(entity, rearer);
}
list.Add(entity);
}
return list;
}
}