//修改用户图片 public void updatePic(User user) { OleDbConnection conn = null; OleDbCommand comm = null; try { conn = DBManage.getConn(); //1.打开数据库 conn.Open(); //2.创建command对象 comm = conn.CreateCommand(); comm.CommandText = "update [user] set picture = @picture where userId = @userId "; List <OleDbParameter> paras = DBManage.getListParameter(new String[] { "@picture", "@userId" }, new Object[] { user.Picture, user.UserId }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); comm.ExecuteNonQuery(); } catch (Exception e) { Console.Write(e.Message); } finally { if (conn != null) { DBManage.closeConn(conn); } } }
//修改浏览次数 public void modifyNum(int postId) { OleDbConnection conn = null; OleDbCommand comm = null; try { conn = DBManage.getConn(); //1.打开数据库 conn.Open(); //2.创建command对象 comm = conn.CreateCommand(); comm.CommandText = "update [post] set browseNum = browseNum + 1 where postId = @postId "; List <OleDbParameter> paras = DBManage.getListParameter(new String[] { "@postId" }, new Object[] { postId }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); comm.ExecuteNonQuery(); } catch (Exception e) { Console.Write(e.Message); throw new Exception(); } finally { if (conn != null) { DBManage.closeConn(conn); } } }
//注册用户 public void add(User user) { OleDbConnection conn = null; OleDbCommand comm = null; try { conn = DBManage.getConn(); //1.打开数据库 conn.Open(); //2.创建command对象 comm = conn.CreateCommand(); comm.CommandText = "insert into [user] values(@userId,@username,@password,@email,@phone,@QQ,@picture)"; //动态设置参数 //List<OleDbParameter> paras = new List<OleDbParameter>(); //paras.Add(new OleDbParameter(){ParameterName = "@userId",Value=user.UserId}); //paras.Add(new OleDbParameter() { ParameterName = "@username", Value = user.Username }); //paras.Add(new OleDbParameter() { ParameterName = "@password", Value = user.Password }); //paras.Add(new OleDbParameter() { ParameterName = "@email", Value = user.Email }); //paras.Add(new OleDbParameter() { ParameterName = "@phone", Value = user.Phone }); //paras.Add(new OleDbParameter() { ParameterName = "@QQ", Value = user.QQ }); //paras.Add(new OleDbParameter() { ParameterName = "@picture", Value = user.Picture }); List <OleDbParameter> paras = DBManage.getListParameter( new String[] { "@userId", "@username", "@password", "@email", "@phone", "@QQ", "@picture" }, new Object[] { user.UserId, user.Username, user.Password, user.Email, user.Phone, user.QQ, user.Picture }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); //3.执行命令 comm.ExecuteNonQuery(); } catch (Exception e) { Console.Write(e.Message); } finally { //4.关闭连接 if (conn != null) { DBManage.closeConn(conn); } } }
//通过ID删除帖子 public bool deletePost(String postId, String userId) { OleDbConnection conn = null; OleDbCommand comm = null; conn = DBManage.getConn(); conn.Open(); comm = conn.CreateCommand(); comm.CommandText = "delete from [post] where postId = @postId and userId = @userId"; List <OleDbParameter> paras = DBManage.getListParameter(new String[] { "@postId", "@userId" }, new Object[] { Convert.ToInt32(postId), userId }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); int flag; try { flag = comm.ExecuteNonQuery(); if (flag == 1) { return(true); } else { return(false); } } catch (Exception e) { return(false); } finally { if (conn != null) { DBManage.closeConn(conn); } } }
//用户登入验证 public User findUser(User user) { OleDbConnection conn = null; OleDbCommand comm = null; OleDbDataReader reader = null; User findUser = null; conn = DBManage.getConn(); //1.打开数据库 conn.Open(); //2.创建command对象 comm = conn.CreateCommand(); comm.CommandText = "select * from [user] where ( username = @username or email = @email ) and password = @password"; //List<OleDbParameter> paras = new List<OleDbParameter>(); //paras.Add(new OleDbParameter() { ParameterName = "@username", Value = user.Username }); //paras.Add(new OleDbParameter() { ParameterName = "@email", Value = user.Username }); //paras.Add(new OleDbParameter() { ParameterName = "@password", Value = user.Password }); List <OleDbParameter> paras = DBManage.getListParameter(new String[] { "@username", "@email", "password" }, new Object[] { user.Username, user.Username, user.Password }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); reader = comm.ExecuteReader(); while (reader.Read()) { findUser = new User(); findUser.UserId = reader.GetString(0); findUser.Username = reader.GetString(1); findUser.Password = reader.GetString(2); findUser.Email = reader.GetString(3); findUser.Phone = reader.GetString(4); findUser.QQ = reader.GetString(5); findUser.Picture = reader.GetString(6); } return(findUser); }
public void add(Reply reply) { OleDbConnection conn = null; OleDbCommand comm = null; try { conn = DBManage.getConn(); //1.打开数据库 conn.Open(); //2.创建command对象 comm = conn.CreateCommand(); comm.CommandText = "insert into reply(replyId,postId,userId,replyMessage,replyTime) values(@replyId,@postId,@userId,@replyMessage,@replyTime)"; List <OleDbParameter> paras = DBManage.getListParameter( new String[] { "@replyId", "@postId", "@userId", "@replyMessage", "@replyTime" }, new Object[] { reply.ReplyId, reply.PostId, reply.UserId, reply.ReplyMessage, reply.ReplyTime.ToString() }); comm.Parameters.AddRange(paras.ToArray <OleDbParameter>()); //3.执行命令 comm.ExecuteNonQuery(); } catch (Exception e) { Console.Write(e.Message); throw new Exception(); } finally { //4.关闭连接 if (conn != null) { DBManage.closeConn(conn); } } }