/// <summary> /// 得到一个对象实体 /// </summary> public starweibo.Model.photos DataRowToModel(DataRow row) { starweibo.Model.photos model = new starweibo.Model.photos(); if (row != null) { if (row["id"] != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } if (row["imageName"] != null) { model.imageName = row["imageName"].ToString(); } if (row["imgPath"] != null) { model.imgPath = row["imgPath"].ToString(); } if (row["uploadDate"] != null && row["uploadDate"].ToString() != "") { model.uploadDate = DateTime.Parse(row["uploadDate"].ToString()); } if (row["photoGroupId"] != null && row["photoGroupId"].ToString() != "") { model.photoGroupId = int.Parse(row["photoGroupId"].ToString()); } if (row["userId"] != null && row["userId"].ToString() != "") { model.userId = int.Parse(row["userId"].ToString()); } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(starweibo.Model.photos model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into photos("); strSql.Append("imageName,imgPath,uploadDate,photoGroupId,userId)"); strSql.Append(" values ("); strSql.Append("@imageName,@imgPath,@uploadDate,@photoGroupId,@userId)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@imageName", SqlDbType.NVarChar, 150), new SqlParameter("@imgPath", SqlDbType.NVarChar, 300), new SqlParameter("@uploadDate", SqlDbType.DateTime), new SqlParameter("@photoGroupId", SqlDbType.Int, 4), new SqlParameter("@userId", SqlDbType.Int, 4) }; parameters[0].Value = model.imageName; parameters[1].Value = model.imgPath; parameters[2].Value = model.uploadDate; parameters[3].Value = model.photoGroupId; parameters[4].Value = model.userId; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(starweibo.Model.photos model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update photos set "); strSql.Append("imageName=@imageName,"); strSql.Append("imgPath=@imgPath,"); strSql.Append("uploadDate=@uploadDate,"); strSql.Append("photoGroupId=@photoGroupId,"); strSql.Append("userId=@userId"); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@imageName", SqlDbType.NVarChar, 150), new SqlParameter("@imgPath", SqlDbType.NVarChar, 300), new SqlParameter("@uploadDate", SqlDbType.DateTime), new SqlParameter("@photoGroupId", SqlDbType.Int, 4), new SqlParameter("@userId", SqlDbType.Int, 4), new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = model.imageName; parameters[1].Value = model.imgPath; parameters[2].Value = model.uploadDate; parameters[3].Value = model.photoGroupId; parameters[4].Value = model.userId; parameters[5].Value = model.id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public starweibo.Model.photos GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,imageName,imgPath,uploadDate,photoGroupId,userId from photos "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; starweibo.Model.photos model = new starweibo.Model.photos(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }