// find all by list public List<MyUser> findAll_List() { string sql = "select * from myuser"; OleDbConnection con = getCon(null); OleDbCommand cmd = new OleDbCommand(sql, con); List<MyUser> list = new List<MyUser>(); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { MyUser bean = new MyUser(); bean.Id = reader.GetInt32(0); bean.Uname = reader.GetString(1); bean.Upass = reader.GetString(2); bean.Userlevel = reader.GetInt32(3); bean.Dept_id = reader.GetInt32(4); list.Add(bean); } //close the con closeAll(con, null, null); if (list.Count <= 0) { return null; } else { return list; } }
// add public void add(MyUser domain) { string sql = "insert into myuser(uname,upass,userlevel,dept_id) " + "values(@uname,@upass,@userlevel,@dept_id) "; OleDbConnection con = getCon(null); OleDbCommand cmd = new OleDbCommand(sql, con); OleDbParameter[] param = {new OleDbParameter("@uname",domain.Uname),new OleDbParameter("@upass",domain.Upass), new OleDbParameter("@userlevel",domain.Userlevel),new OleDbParameter("@dept_id",domain.Dept_id)}; cmd.Parameters.AddRange(param); cmd.ExecuteNonQuery(); // close the con closeAll(con,cmd, null); }
public static void test_MyUserDao() { MyUserDao dao = new MyUserDao(); MyUser bean = new MyUser(); // add //bean.Uname = "肖云"; bean.Upass = "******"; bean.Userlevel = 0; bean.Dept_id = 3; // dao.add(bean); // update //bean.Id = 3; bean.Uname = "阿曼达"; bean.Upass = "******"; bean.Userlevel = 0; bean.Dept_id = 3; //dao.update(bean); // load // bean = dao.load(3); // Console.WriteLine("{0} {1} {2} {3} {4}",bean.Id,bean.Uname,bean.Upass,bean.Userlevel,bean.Dept_id); // Console.Read(); // find all //DataSet da = dao.findAll(); //DataTableReader reader = da.CreateDataReader(); //while(reader.Read()) //Console.WriteLine("{0} {1} {2} {3} {4}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2),reader.GetInt32(3),reader.GetInt32(4)); //Console.Read(); // load by name // bean = dao.loadByName("阿曼达"); // Console.WriteLine("{0} {1} {2} {3} {4}", bean.Id, bean.Uname, bean.Upass, bean.Userlevel, bean.Dept_id); // Console.Read(); // delete dao.delete(5); }
// update public void update(MyUser domain) { string sql = "update myuser set uname=@uname,upass=@upass,userlevel=@userlevel,dept_id=@dept_id where id=@id"; OleDbConnection con = getCon(null); OleDbCommand cmd = new OleDbCommand(sql, con); OleDbParameter[] param = {new OleDbParameter("@uname",domain.Uname),new OleDbParameter("@upass",domain.Upass), new OleDbParameter("@userlevel",domain.Userlevel),new OleDbParameter("@dept_id",domain.Dept_id), new OleDbParameter("@id",domain.Id)}; cmd.Parameters.AddRange(param); cmd.ExecuteNonQuery(); // close the con closeAll(con,cmd,null); }
// load by name public MyUser loadByName(string uname) { string sql = "select * from myuser where uname=@uname"; OleDbConnection con = getCon(null); OleDbCommand cmd = new OleDbCommand(sql,con); OleDbParameter param = new OleDbParameter("@uname", uname); cmd.Parameters.Add(param); MyUser domain = null; OleDbDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { domain = new MyUser(); domain.Id = reader.GetInt32(0); domain.Uname = reader.GetString(1); domain.Upass = reader.GetString(2); domain.Userlevel = reader.GetInt32(3); domain.Dept_id = reader.GetInt32(4); } // close the con closeAll(con,cmd,reader); return domain; }