public ActionResult DeleteFile(string fileName, int repoId,string repoOwner, CreateViewModel model) { try { fileName = fileName.Trim(); string connStr = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20160108044733.mdf;Initial Catalog=aspnet-WebApplication2-20160108044733;Integrated Security=True"; SqlConnection conn = new SqlConnection(connStr); try { //пробуем подключится conn.Open(); } catch (SqlException se) { ModelState.AddModelError("", "can't open connection" + se); return RedirectToAction("Edit", routeValues: new { id = repoId, model = model }); } string query = "DELETE FROM Files" + " where Name = @Id AND Repo=@repoId"; SqlCommand cmd = new SqlCommand(query, conn); SqlParameter param = new SqlParameter(); param.ParameterName = "@Id"; param.Value = fileName; param.SqlDbType = SqlDbType.NVarChar; cmd.Parameters.Add(param); param = new SqlParameter(); param.ParameterName = "@repoId"; param.Value = repoId; param.SqlDbType = SqlDbType.Int; cmd.Parameters.Add(param); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { ModelState.AddModelError("", "Can't update. " + ex); return RedirectToAction("Edit", routeValues: new { id = repoId, model = model }); } conn.Close(); conn.Dispose(); FileInfo File = new FileInfo(Server.MapPath("~/Repos/" + repoOwner + "/" +repoId+"/"+ fileName)); File.Delete(); ViewData["Message"] = "Success"; return RedirectToAction("Edit", routeValues: new { id = repoId, model = model }); } catch (Exception ex) { ModelState.AddModelError("", "Can't. " + ex); return RedirectToAction("Edit", routeValues: new { id = repoId, model = model }); } }
public ActionResult Create(CreateViewModel model) { try { string connStr = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20160108044733.mdf;Initial Catalog=aspnet-WebApplication2-20160108044733;Integrated Security=True"; SqlConnection conn = new SqlConnection(connStr); try { //пробуем подключится conn.Open(); } catch (SqlException se) { ModelState.AddModelError("", "can't open connection"+se); return View(model); } string query = "INSERT INTO Repositories (Name,Owner,tags,LastChangeR) VALUES " + "(@Name, @Owner,@tags,@time)"; SqlCommand cmd = new SqlCommand(query, conn); SqlParameter param = new SqlParameter(); param.ParameterName = "@Name"; param.Value = model.Name; param.SqlDbType = SqlDbType.NVarChar; cmd.Parameters.Add(param); param = new SqlParameter(); param.ParameterName = "@Owner"; param.Value = User.Identity.GetUserId(); param.SqlDbType = SqlDbType.NVarChar; cmd.Parameters.Add(param); param = new SqlParameter(); param.ParameterName = "@tags"; param.Value = model.tags; param.SqlDbType = SqlDbType.NVarChar; cmd.Parameters.Add(param); param = new SqlParameter(); param.ParameterName = "@time"; param.Value = System.DateTime.Now; param.SqlDbType = SqlDbType.DateTime; cmd.Parameters.Add(param); try { cmd.ExecuteNonQuery(); } catch(Exception ex) { ModelState.AddModelError("", "Can't update. "+ ex); return RedirectToAction("Profile","Account"); } string query1="SELECT * FROM Repositories WHERE Name='"+model.Name+"';"; SqlCommand cmd1 = new SqlCommand(query1, conn); SqlDataReader dr=cmd1.ExecuteReader(CommandBehavior.CloseConnection); dr.Read(); string dirName = dr.GetValue(0).ToString(); conn.Close(); conn.Dispose(); if (Directory.Exists("~/Repos/" + User.Identity.GetUserId())) { DirectoryInfo Dir = new DirectoryInfo(Server.MapPath("~/Repos/" + User.Identity.GetUserId())); Dir.CreateSubdirectory(dirName); } else { DirectoryInfo Dir = new DirectoryInfo(Server.MapPath("~/Repos/" + User.Identity.GetUserId())); Dir.Create(); Dir.CreateSubdirectory(dirName); } ViewData["Message"] = "Success"; return View(model); } catch(Exception ex) { ModelState.AddModelError("", "Smth wrong." + ex); return View(model); } }
// // GET: /Repository/Delete/5 public ActionResult Delete(int id,CreateViewModel model) { try { string connStr = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20160108044733.mdf;Initial Catalog=aspnet-WebApplication2-20160108044733;Integrated Security=True"; SqlConnection conn = new SqlConnection(connStr); try { //пробуем подключится conn.Open(); } catch (SqlException se) { ModelState.AddModelError("", "can't open connection" + se); return RedirectToAction("Profile", "Account", model); } string query = "DELETE FROM Repositories" + " where Id = @Id"; SqlCommand cmd = new SqlCommand(query, conn); SqlParameter param = new SqlParameter(); param.ParameterName = "@Id"; param.Value = id; param.SqlDbType = SqlDbType.Int; cmd.Parameters.Add(param); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { ModelState.AddModelError("", "Can't update. " + ex); return RedirectToAction("Profile", "Account", model); } conn.Close(); conn.Dispose(); DirectoryInfo Dir = new DirectoryInfo(Server.MapPath("~/Repos/" + User.Identity.GetUserId() +"/"+id)); Dir.Delete(); ViewData["Message"] = "Success"; return RedirectToAction("Profile", "Account"); } catch(Exception ex) { ModelState.AddModelError("", "Can't. " + ex); return RedirectToAction("Profile", "Account", model); } }