/// <summary> /// 得到一个对象实体 /// </summary> public dbamet.Model.news GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,title,[content],type,category,time,[file] from news "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; dbamet.Model.news model = new dbamet.Model.news(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "") { model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString()); } if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "") { model.title = ds.Tables[0].Rows[0]["title"].ToString(); } if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "") { model.content = ds.Tables[0].Rows[0]["content"].ToString(); } if (ds.Tables[0].Rows[0]["type"] != null && ds.Tables[0].Rows[0]["type"].ToString() != "") { model.type = ds.Tables[0].Rows[0]["type"].ToString(); } if (ds.Tables[0].Rows[0]["category"] != null && ds.Tables[0].Rows[0]["category"].ToString() != "") { model.category = ds.Tables[0].Rows[0]["category"].ToString(); } if (ds.Tables[0].Rows[0]["time"] != null && ds.Tables[0].Rows[0]["time"].ToString() != "") { model.time = DateTime.Parse(ds.Tables[0].Rows[0]["time"].ToString()); } if (ds.Tables[0].Rows[0]["file"] != null && ds.Tables[0].Rows[0]["file"].ToString() != "") { model.file = ds.Tables[0].Rows[0]["file"].ToString(); } return(model); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(dbamet.Model.news model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update news set "); strSql.Append("title=@title,"); strSql.Append("content=@content,"); strSql.Append("type=@type,"); strSql.Append("category=@category,"); strSql.Append("time=@time,"); strSql.Append("[file]=@file"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@title", SqlDbType.VarChar, 50), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@type", SqlDbType.VarChar, 50), new SqlParameter("@category", SqlDbType.VarChar, 50), new SqlParameter("@time", SqlDbType.DateTime), new SqlParameter("@file", SqlDbType.NVarChar, 50), new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = model.title; parameters[1].Value = model.content; parameters[2].Value = model.type; parameters[3].Value = model.category; parameters[4].Value = model.time; parameters[5].Value = model.file; parameters[6].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(dbamet.Model.news model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into news("); strSql.Append("title,[content],type,category,time,[file])"); strSql.Append(" values ("); strSql.Append("@title,@content,@type,@category,@time,@file)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@title", SqlDbType.VarChar, 50), new SqlParameter("@content", SqlDbType.NText), new SqlParameter("@type", SqlDbType.VarChar, 50), new SqlParameter("@category", SqlDbType.VarChar, 50), new SqlParameter("@time", SqlDbType.DateTime), new SqlParameter("@file", SqlDbType.NVarChar, 50) }; parameters[0].Value = model.title; parameters[1].Value = model.content; parameters[2].Value = model.type; parameters[3].Value = model.category; parameters[4].Value = model.time; parameters[5].Value = model.file; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }