public void Add(T entity)
{
//保存字段名
string st = string.Empty;
//保存值
string sv = string.Empty;
//获取表名
string Name = typeof(T).Name;
//根据实例获取属性和值
Type type = entity.GetType();
List<SqlParameter> sqlParameters = new List<SqlParameter>();
foreach (var item in type.GetProperties())
{
//检索指定类型用于特定成员的自定义属性
KeyAttribute attr = item.GetCustomAttribute<KeyAttribute>();
if (attr == null)
{
//获取属性名,拼接字段
if (!string.IsNullOrEmpty(st))
{
st += ",";
}
st += item.Name;
if (!string.IsNullOrEmpty(sv))
{
sv += ",";
}
sv += ("@" + item.Name);
sqlParameters.Add(new SqlParameter("@" + item.Name, item.GetValue(entity)));
}
}
string sql = string.Format("insert into {0} ({1}) values({2})", Name, st, sv);
}