/// <summary> /// 得到一个对象实体 /// </summary> public static IMSG GetModel(int ID, int msg_to) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 ID,msg_from,msg_to,msg_content,msg_addtime,msg_title from Msg "); strSql.Append(" where ID=@ID and msg_to = @msg_to"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@msg_to", SqlDbType.Int, 4) }; parameters[0].Value = ID; parameters[1].Value = msg_to; IMSG model = new IMSG(); DataSet ds = DataBase.ExecuteDataset(CommandType.Text, strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["ID"].ToString() != "") { model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString()); } if (ds.Tables[0].Rows[0]["msg_from"].ToString() != "") { model.msg_from = int.Parse(ds.Tables[0].Rows[0]["msg_from"].ToString()); } if (ds.Tables[0].Rows[0]["msg_to"].ToString() != "") { model.msg_to = int.Parse(ds.Tables[0].Rows[0]["msg_to"].ToString()); } model.msg_content = ds.Tables[0].Rows[0]["msg_content"].ToString(); if (ds.Tables[0].Rows[0]["msg_addtime"].ToString() != "") { model.msg_addtime = DateTime.Parse(ds.Tables[0].Rows[0]["msg_addtime"].ToString()); } model.msg_title = ds.Tables[0].Rows[0]["msg_title"].ToString(); return(model); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public static bool Update(IMSG model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Msg set "); strSql.Append("msg_from=@msg_from,"); strSql.Append("msg_to=@msg_to,"); strSql.Append("msg_content=@msg_content,"); strSql.Append("msg_addtime=@msg_addtime,"); strSql.Append("msg_title=@msg_title,"); strSql.Append("isRead=@isRead"); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@msg_from", SqlDbType.Int, 4), new SqlParameter("@msg_to", SqlDbType.Int, 4), new SqlParameter("@msg_content", SqlDbType.NVarChar, 2000), new SqlParameter("@msg_addtime", SqlDbType.DateTime), new SqlParameter("@msg_title", SqlDbType.NVarChar, 50), new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@isRead", SqlDbType.Bit, 1) }; parameters[0].Value = model.msg_from; parameters[1].Value = model.msg_to; parameters[2].Value = model.msg_content; parameters[3].Value = model.msg_addtime; parameters[4].Value = model.msg_title; parameters[5].Value = model.ID; parameters[6].Value = model.isRead; int rows = DataBase.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 增加一条数据 /// </summary> public static int Add(IMSG model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Msg("); strSql.Append("msg_from,msg_to,msg_content,msg_addtime,msg_title,msg_fromname)"); strSql.Append(" values ("); strSql.Append("@msg_from,@msg_to,@msg_content,@msg_addtime,@msg_title,@msg_fromname)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@msg_from", SqlDbType.Int, 4), new SqlParameter("@msg_to", SqlDbType.Int, 4), new SqlParameter("@msg_content", SqlDbType.NVarChar, 2000), new SqlParameter("@msg_addtime", SqlDbType.DateTime), new SqlParameter("@msg_title", SqlDbType.NVarChar, 50), new SqlParameter("@msg_fromname", SqlDbType.NVarChar, 50) }; parameters[0].Value = model.msg_from; parameters[1].Value = model.msg_to; parameters[2].Value = model.msg_content; parameters[3].Value = model.msg_addtime; parameters[4].Value = model.msg_title; parameters[5].Value = model.msg_fromname; object obj = DataBase.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }