public ActionResult Create(hotel hotel, HttpPostedFileBase tourism_photo) { string h_id = db.Database.SqlQuery <string>("Select dbo.GethotelId()").FirstOrDefault(); hotel.hotel_id = h_id; string fileName = ""; if (tourism_photo != null) { if (tourism_photo.ContentLength > 0) { fileName = System.IO.Path.GetFileName(tourism_photo.FileName); tourism_photo.SaveAs(Server.MapPath("~/images/hotel/" + fileName)); } } tourism_photo tp = new tourism_photo(); tp.tourism_photo1 = fileName; tp.tourism_id = hotel.hotel_id; db.hotels.Add(hotel); db.tourism_photo.Add(tp); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(spot spot, HttpPostedFileBase[] tourism_photo) { //表單內容 string act_id = db.Database.SqlQuery <string>("Select dbo.GetspotId()").FirstOrDefault(); spot.spot_id = act_id; spot.update_date = DateTime.Now; spot.page_status = false; //圖片 string fileName = ""; for (int i = 0; i < tourism_photo.Length; i++) { HttpPostedFileBase f = tourism_photo[i]; if (f != null) { if (f.ContentLength > 0) { string t = tourism_photo[i].FileName; fileName = spot.spot_id + "_" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + (i + 1).ToString() + Path.GetExtension(t); f.SaveAs(Server.MapPath("~/images/spot/" + fileName)); tourism_photo tp = new tourism_photo(); tp.tourism_photo1 = fileName; tp.tourism_id = spot.spot_id; db.tourism_photo.Add(tp); } } } db.spot.Add(spot); db.SaveChanges(); return(RedirectToAction("Index", new { page = Session["pg"] })); }
// GET: ImportTEST public void LoadJson() { using (StreamReader r = new StreamReader(@"D:\TravelCAT\TravelCat\spot_C_f.json")) { var json = r.ReadToEnd(); List <Rootobject> rt = JsonConvert.DeserializeObject <List <Rootobject> >(json); for (int i = 0; i < rt.Count; i++) { spot items = new spot(); items.spot_id = db.Database.SqlQuery <string>("Select dbo.GetspotId()").FirstOrDefault(); items.spot_intro = rt[i].Toldescribe; items.spot_tel = rt[i].Tel; items.spot_title = rt[i].Name; items.latitude = rt[i].Py.ToString(); items.longitude = rt[i].Px.ToString(); items.addition_note = rt[i].Remarks; items.address_detail = rt[i].Add; items.city = rt[i].Region; items.district = rt[i].Town; items.open_time = rt[i].Opentime; items.ticket_info = rt[i].Ticketinfo; items.update_date = rt[i].Changetime; items.travelling_info = rt[i].Travellinginfo; if (String.IsNullOrEmpty(rt[i].Picture1) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.spot_id; tp.tourism_photo1 = rt[i].Picture1; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture2) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.spot_id; tp.tourism_photo1 = rt[i].Picture2; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture3) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.spot_id; tp.tourism_photo1 = rt[i].Picture3; db.tourism_photo.Add(tp); } db.spot.Add(items); db.SaveChanges(); } Console.WriteLine("success"); } }
public ActionResult Edit(string id, SpotPhotoViewModel spotPhotoViewModel, HttpPostedFileBase[] tourism_photo, String oldImg) { spotPhotoViewModel.spot.update_date = DateTime.Now; db.Entry(spotPhotoViewModel.spot).State = EntityState.Modified; db.SaveChanges(); //圖片 string fileName = ""; var tp1 = db.tourism_photo.Where(m => m.tourism_id == id).ToList(); for (int i = 0; i < 3; i++) { if (tourism_photo[i] != null) { //如果有新增檔案到input if (tourism_photo[i].ContentLength > 0) { //改名 string t = tourism_photo[i].FileName; fileName = spotPhotoViewModel.spot.spot_id + "_" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + (i + 1).ToString() + Path.GetExtension(t); if (i < tp1.Count) //如果原有紀錄 { //刪掉原檔案 System.IO.File.Delete(Server.MapPath("~/images/spot/" + tp1[i].tourism_photo1)); tourism_photo[i].SaveAs(Server.MapPath("~/images/spot/" + fileName)); //將檔案存到該資料夾 //改掉資料庫檔案 tp1[i].tourism_photo1 = fileName; } else //如果原本沒有 { tourism_photo tp = new tourism_photo(); tp.tourism_photo1 = fileName; tourism_photo[i].SaveAs(Server.MapPath("~/images/spot/" + fileName)); tp.tourism_id = spotPhotoViewModel.spot.spot_id; db.tourism_photo.Add(tp); } } //else //{ //} } //else //if(String.IsNullOrEmpty(oldImg)) //{ // tourism_photo tp = new tourism_photo(); // tp.tourism_photo1 = fileName; // tp.tourism_id = spotPhotoViewModel.spot.spot_id; // db.tourism_photo.Add(tp); //} } db.SaveChanges(); return(RedirectToAction("Index", new { page = Session["pg"] })); }
public void LoadJson() { using (StreamReader r = new StreamReader(@"D:\TravelCAT\TravelCat\hotel_C_f.json")) { var json = r.ReadToEnd(); tourism_photo tp = new tourism_photo(); List <Rootobject> rt = JsonConvert.DeserializeObject <List <Rootobject> >(json); for (int i = 0; i < rt.Count; i++) { hotel items = new hotel(); items.hotel_id = db.Database.SqlQuery <string>("Select dbo.GethotelId()").FirstOrDefault(); items.hotel_intro = rt[i].Description; items.hotel_tel = rt[i].Tel; items.hotel_title = rt[i].Name; items.latitude = rt[i].Py.ToString(); items.longitude = rt[i].Px.ToString(); items.website = rt[i].Website; items.address_detail = rt[i].Add; items.city = rt[i].Region; items.district = rt[i].Town; if (rt[i].Picture1 != "") { tp.tourism_id = items.hotel_id; tp.tourism_photo1 = rt[i].Picture1; db.tourism_photo.Add(tp); } if (rt[i].Picture2 != "") { tp.tourism_id = items.hotel_id; tp.tourism_photo1 = rt[i].Picture2; db.tourism_photo.Add(tp); } if (rt[i].Picture3 != "") { tp.tourism_id = items.hotel_id; tp.tourism_photo1 = rt[i].Picture3; db.tourism_photo.Add(tp); } db.hotel.Add(items); db.SaveChanges(); } Console.WriteLine("success"); } }
[HttpPost] //預設就是httpget,這裡是要讓他新增資料到資料庫 public ActionResult Create(activity activity, HttpPostedFileBase[] tourism_photo) //多載(overloading/overloading) { string act_id = db.Database.SqlQuery <string>("Select dbo.GetactivityId()").FirstOrDefault(); activity.activity_id = act_id; activity.page_status = false; //圖片 string fileName = ""; for (int i = 0; i < tourism_photo.Length; i++) { HttpPostedFileBase f = tourism_photo[i]; if (f != null) { if (f.ContentLength > 0) { string t = tourism_photo[i].FileName; fileName = activity.activity_id + "_" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + (i + 1).ToString() + Path.GetExtension(t); f.SaveAs(Server.MapPath("~/images/activity/" + fileName)); tourism_photo tp = new tourism_photo(); tp.tourism_photo1 = fileName; tp.tourism_id = activity.activity_id; db.tourism_photo.Add(tp); } } } db.activity.Add(activity); db.SaveChanges(); //try //{ // db.SaveChanges(); // } //catch (DbEntityValidationException ex) //{ // var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); //var getFullMessage = string.Join("; ", entityError); //var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); //NLog //return View(exceptionMessage.ToString()); //} return(RedirectToAction("Index", new { page = Session["pg"] })); }
[HttpPost] //預設就是httpget,這裡是要讓他新增資料到資料庫 public ActionResult Create(activity activity, HttpPostedFileBase tourism_photo) //多載(overloading/overloading) { string act_id = db.Database.SqlQuery <string>("Select dbo.GetactivityId()").FirstOrDefault(); activity.activity_id = act_id; //處理圖檔上傳 string fileName = ""; if (tourism_photo != null) { if (tourism_photo.ContentLength > 0) { fileName = System.IO.Path.GetFileName(tourism_photo.FileName); //取得檔案的檔名(主檔名+副檔名) tourism_photo.SaveAs(Server.MapPath("~/images/activity/" + fileName)); //將檔案存到該資料夾 } } //end tourism_photo tp = new tourism_photo(); tp.tourism_photo1 = fileName; tp.tourism_id = activity.activity_id; db.activities.Add(activity); db.SaveChanges(); db.tourism_photo.Add(tp); try { db.SaveChanges(); } catch (DbEntityValidationException ex) { var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var getFullMessage = string.Join("; ", entityError); var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); //NLog return(View(exceptionMessage.ToString())); } return(RedirectToAction("Index")); }
// GET: ImportTEST public void LoadJson() { using (StreamReader r = new StreamReader(@"D:\TravelCAT\TravelCat\activity_C_f.json")) { var json = r.ReadToEnd(); List <Rootobject> rt = JsonConvert.DeserializeObject <List <Rootobject> >(json); for (int i = 0; i < rt.Count; i++) { activity items = new activity(); items.activity_id = db.Database.SqlQuery <string>("Select dbo.GetactivityId()").FirstOrDefault(); items.activity_intro = rt[i].Description; items.activity_tel = rt[i].Tel; items.activity_title = rt[i].Name; items.latitude = rt[i].Py.ToString(); items.longitude = rt[i].Px.ToString(); items.website = rt[i].Website; items.address_detail = rt[i].Add; items.city = rt[i].Region; items.district = rt[i].Town; items.begin_date = rt[i].Start.ToString(); items.end_date = rt[i].End.ToString(); items.organizer = rt[i].Org; items.transport_info = rt[i].Travellinginfo; //if (rt[i].Picture1 != "") //{ // tourism_photo tp = new tourism_photo(); // tp.tourism_id = items.activity_id; // tp.tourism_photo1 = rt[i].Picture1; // db.tourism_photo.Add(tp); //} //if (rt[i].Picture2 != "") //{ // tourism_photo tp = new tourism_photo(); // tp.tourism_id = items.activity_id; // tp.tourism_photo1 = rt[i].Picture2; // db.tourism_photo.Add(tp); //} //if (rt[i].Picture3 != "") //{ // tourism_photo tp = new tourism_photo(); // tp.tourism_id = items.activity_id; // tp.tourism_photo1 = rt[i].Picture3; // db.tourism_photo.Add(tp); //} //db.activity.Add(items); //try //{ // db.SaveChanges(); //} //catch (DbEntityValidationException ex) //{ // var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); // var getFullMessage = string.Join("; ", entityError); // var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); // //NLog // Console.WriteLine(exceptionMessage.ToString()); //} if (String.IsNullOrEmpty(rt[i].Picture1) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.activity_id; tp.tourism_photo1 = rt[i].Picture1; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture2) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.activity_id; tp.tourism_photo1 = rt[i].Picture2; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture3) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.activity_id; tp.tourism_photo1 = rt[i].Picture3; db.tourism_photo.Add(tp); } db.activity.Add(items); db.SaveChanges(); } Console.WriteLine("success"); } }
public void LoadJson() { using (StreamReader r = new StreamReader(@"D:\TravelCAT\TravelCat\restaurant_C_f.json")) { var json = r.ReadToEnd(); List <Rootobject> rt = JsonConvert.DeserializeObject <List <Rootobject> >(json); for (int i = 0; i < rt.Count; i++) { restaurant items = new restaurant(); items.restaurant_id = db.Database.SqlQuery <string>("Select dbo.GetrestId()").FirstOrDefault(); items.restaurant_intro = rt[i].Description; items.restaurant_tel = rt[i].Tel; items.restaurant_title = rt[i].Name; items.latitude = rt[i].Py.ToString(); items.longitude = rt[i].Px.ToString(); items.open_time = rt[i].Opentime; items.address_detail = rt[i].Add; items.city = rt[i].Region; items.district = rt[i].Town; //if (String.IsNullOrEmpty(rt[i].Picture1) != true) //{ // tp.tourism_id = items.restaurant_id; // tp.tourism_photo1 = rt[i].Picture1; // db.tourism_photo.Add(tp); //} //if (String.IsNullOrEmpty(rt[i].Picture2) != true) //{ // tp.tourism_id = items.restaurant_id; // tp.tourism_photo1 = rt[i].Picture2; // db.tourism_photo.Add(tp); //} //if (String.IsNullOrEmpty(rt[i].Picture3) != true) //{ // tp.tourism_id = items.restaurant_id; // tp.tourism_photo1 = rt[i].Picture3; // db.tourism_photo.Add(tp); //} if (String.IsNullOrEmpty(rt[i].Picture1) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.restaurant_id; tp.tourism_photo1 = rt[i].Picture1; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture2) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.restaurant_id; tp.tourism_photo1 = rt[i].Picture2; db.tourism_photo.Add(tp); } if (String.IsNullOrEmpty(rt[i].Picture3) != true) { tourism_photo tp = new tourism_photo(); tp.tourism_id = items.restaurant_id; tp.tourism_photo1 = rt[i].Picture3; db.tourism_photo.Add(tp); } db.restaurant.Add(items); db.SaveChanges(); } Console.WriteLine("success"); } }