/// <summary> /// 增加一条数据 /// </summary> public int Add(Travels model) { StringBuilder strSql=new StringBuilder(); strSql.Append("insert into Travels("); strSql.Append("UserId,TravelName,CreateTime,UpdateTime,CoverImage,IsDelete)"); strSql.Append(" values ("); strSql.Append("@UserId,@TravelName,@CreateTime,@UpdateTime,@CoverImage,@IsDelete)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@UserId", SqlDbType.Int,4), new SqlParameter("@TravelName", SqlDbType.VarChar,50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@CoverImage", SqlDbType.VarChar,100), new SqlParameter("@IsDelete", SqlDbType.Bit,1)}; parameters[0].Value = model.UserId; parameters[1].Value = model.TravelName; parameters[2].Value = model.CreateTime; parameters[3].Value = model.UpdateTime; parameters[4].Value = model.CoverImage; parameters[5].Value = model.IsDelete; object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Travels DataRowToModel(DataRow row) { Travels model=new Travels(); if (row != null) { if(row["Id"]!=null && row["Id"].ToString()!="") { model.Id=int.Parse(row["Id"].ToString()); } if(row["UserId"]!=null && row["UserId"].ToString()!="") { model.UserId=int.Parse(row["UserId"].ToString()); } if(row["TravelName"]!=null) { model.TravelName=row["TravelName"].ToString(); } if(row["CreateTime"]!=null && row["CreateTime"].ToString()!="") { model.CreateTime=DateTime.Parse(row["CreateTime"].ToString()); } if(row["UpdateTime"]!=null && row["UpdateTime"].ToString()!="") { model.UpdateTime=DateTime.Parse(row["UpdateTime"].ToString()); } if(row["CoverImage"]!=null) { model.CoverImage=row["CoverImage"].ToString(); } if(row["IsDelete"]!=null && row["IsDelete"].ToString()!="") { if((row["IsDelete"].ToString()=="1")||(row["IsDelete"].ToString().ToLower()=="true")) { model.IsDelete=true; } else { model.IsDelete=false; } } } return model; }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Travels model) { return dal.Add(model); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Travels model) { return dal.Update(model); }
public List<TravelVm> GetMyTravels(int page, int count, int userid) { var list=new List<TravelVm>(); var ds = dal.GetMyTravels(page,count,userid); if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count <= 0) return list; foreach (DataRow dataRow in ds.Tables[0].Rows) { var partId = Int32.Parse(dataRow["id"].ToString()); var travelId = 0; if (dataRow["TravelId"] != DBNull.Value && !string.IsNullOrEmpty(dataRow["TravelId"].ToString())) { travelId = Int32.Parse(dataRow["TravelId"].ToString()); } var partType = Int32.Parse(dataRow["PartType"].ToString()); var description = dataRow["Description"].ToString(); var partUrl = dataRow["PartUrl"]!=DBNull.Value?dataRow["PartUrl"].ToString():""; var longitude = dataRow["Longitude"] != DBNull.Value ? Decimal.Parse(dataRow["Longitude"].ToString()) : 0; var latitude = dataRow["Latitude"] != DBNull.Value ? Decimal.Parse(dataRow["Latitude"].ToString()) : 0; var height = dataRow["Height"] != DBNull.Value ? Decimal.Parse(dataRow["Height"].ToString()) : 0; var area = dataRow["Area"] != DBNull.Value ? dataRow["Area"].ToString() : ""; var createtime = DateTime.Parse(dataRow["CreateTime"].ToString()); var travel=new Travels(); if (travelId>0) { travel = GetModelByCache(travelId); } var vm = new TravelVm() { TravelName = travel != null ? travel.TravelName : "", CoverImage = travel != null ? travel.CoverImage : "", Id = partId, TravelId = travelId, PartType = partType, PartUrl = partUrl, Description = description, Longitude = longitude, Latitude = latitude, Height = height, Area = area, CreateTime = createtime, }; list.Add(vm); } return list; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Travels model) { StringBuilder strSql=new StringBuilder(); strSql.Append("update Travels set "); strSql.Append("UserId=@UserId,"); strSql.Append("TravelName=@TravelName,"); strSql.Append("CreateTime=@CreateTime,"); strSql.Append("UpdateTime=@UpdateTime,"); strSql.Append("CoverImage=@CoverImage,"); strSql.Append("IsDelete=@IsDelete"); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@UserId", SqlDbType.Int,4), new SqlParameter("@TravelName", SqlDbType.VarChar,50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@CoverImage", SqlDbType.VarChar,100), new SqlParameter("@IsDelete", SqlDbType.Bit,1), new SqlParameter("@Id", SqlDbType.Int,4)}; parameters[0].Value = model.UserId; parameters[1].Value = model.TravelName; parameters[2].Value = model.CreateTime; parameters[3].Value = model.UpdateTime; parameters[4].Value = model.CoverImage; parameters[5].Value = model.IsDelete; parameters[6].Value = model.Id; int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); if (rows > 0) { return true; } else { return false; } }
/// <summary> /// 得到一个对象实体 /// </summary> public Travels GetModel(int Id) { StringBuilder strSql=new StringBuilder(); strSql.Append("select top 1 Id,UserId,TravelName,CreateTime,UpdateTime,CoverImage,IsDelete from Travels "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int,4) }; parameters[0].Value = Id; Travels model=new Travels(); DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); if(ds.Tables[0].Rows.Count>0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } }
public string Create(string userId, string travelName) { if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(travelName)) { return HttpRequestResult.StateNotNull; } int useridInt; if (Int32.TryParse(userId, out useridInt) && useridInt > 0) { var model = new Travels { TravelName = travelName, UserId = useridInt, IsDelete = false, CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; var flag = _travelsBll.Add(model); return flag > 0 ? HttpRequestResult.StateOk : HttpRequestResult.StateError; } return HttpRequestResult.StateError; }