求泛型集合的增删改查的代码(c#)
2个回答
展开全部
public abstract class BaseDAL<T>:IBaseDAL<T> where T:class
{
net11Entities db = new net11Entities();
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public List<T> Select()
{
return db.Set<T>().ToList();
}
/// <summary>
/// 添加
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Add(T m)
{
db.Set<T>().Add(m);
return db.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Del(Expression<Func<T,bool>> where)
{
try
{
//DBContextFactory.GetDbContext();
var s= db.Set<T>().Where(where).FirstOrDefault();
db.Set<T>().Remove(s);
//db.Set<T>().Remove(m);
return db.SaveChanges();
}
catch(Exception se)
{
return 0;
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Upt(T m)
{
db.Set<T>().Attach(m);
db.Entry(m).State = System.Data.EntityState.Modified;
return db.SaveChanges();
}
}
不知道是不是你想要的。。。。
{
net11Entities db = new net11Entities();
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public List<T> Select()
{
return db.Set<T>().ToList();
}
/// <summary>
/// 添加
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Add(T m)
{
db.Set<T>().Add(m);
return db.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Del(Expression<Func<T,bool>> where)
{
try
{
//DBContextFactory.GetDbContext();
var s= db.Set<T>().Where(where).FirstOrDefault();
db.Set<T>().Remove(s);
//db.Set<T>().Remove(m);
return db.SaveChanges();
}
catch(Exception se)
{
return 0;
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Upt(T m)
{
db.Set<T>().Attach(m);
db.Entry(m).State = System.Data.EntityState.Modified;
return db.SaveChanges();
}
}
不知道是不是你想要的。。。。
2013-07-09
展开全部
public static List<Pet> GetPet()
{
GetConn();
string sql = "select * from pets";
comm = new SqlCommand(sql, conn);
reader=comm.ExecuteReader();
List<Pet> list=new List<Pet>();
while(reader.Read())
{
Pet pet = new Pet();
pet.Id =Convert.ToInt32(reader["id"]);
pet.Name = Convert.ToString(reader["name"]);
pet.CreatDate = Convert.ToDateTime(reader["creatDate"]);
pet.Reamerk = reader["Reamerk"].ToString();
list.Add(pet);
}
return list;
}
public static int DeletePet(int id)
{
GetConn();
string sql = "delete from pets where id=" + id;
comm = new SqlCommand(sql, conn);
return comm.ExecuteNonQuery();
}
public static int InsertPet(Pet pet)
{
GetConn();
string sql = "insert into Pets values ('"+pet.Name+"','"+pet.CreatDate+"','"+pet.Reamerk+"')";
comm = new SqlCommand(sql,conn);
return comm.ExecuteNonQuery();
}
public static int UpdatePet(Pet pet)
{
GetConn();
string sql = "update pets set Name='" + pet.Name + "',CreatDate='" + pet.CreatDate + "',Reamerk='" + pet.Reamerk + "' where id="+pet.Id+"";
comm = new SqlCommand(sql, conn);
return comm.ExecuteNonQuery();
}
{
GetConn();
string sql = "select * from pets";
comm = new SqlCommand(sql, conn);
reader=comm.ExecuteReader();
List<Pet> list=new List<Pet>();
while(reader.Read())
{
Pet pet = new Pet();
pet.Id =Convert.ToInt32(reader["id"]);
pet.Name = Convert.ToString(reader["name"]);
pet.CreatDate = Convert.ToDateTime(reader["creatDate"]);
pet.Reamerk = reader["Reamerk"].ToString();
list.Add(pet);
}
return list;
}
public static int DeletePet(int id)
{
GetConn();
string sql = "delete from pets where id=" + id;
comm = new SqlCommand(sql, conn);
return comm.ExecuteNonQuery();
}
public static int InsertPet(Pet pet)
{
GetConn();
string sql = "insert into Pets values ('"+pet.Name+"','"+pet.CreatDate+"','"+pet.Reamerk+"')";
comm = new SqlCommand(sql,conn);
return comm.ExecuteNonQuery();
}
public static int UpdatePet(Pet pet)
{
GetConn();
string sql = "update pets set Name='" + pet.Name + "',CreatDate='" + pet.CreatDate + "',Reamerk='" + pet.Reamerk + "' where id="+pet.Id+"";
comm = new SqlCommand(sql, conn);
return comm.ExecuteNonQuery();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询