private string insertBuildItems(BuildItem b) { SqlDataReader rdr = null;// setup the sql reader SqlConnection buildItemsDB = new SqlConnection();// get the conn buildItemsDB.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BuildsConnectionString"].ToString(); buildItemsDB.Open();// open the database try { // check to see if it exists. If this one already exists, then update it with the new content, else insert it string itemExists = "INSERT INTO BuildItems (caption, fileName, orderNumber, status, thumbnailPath, timeStamp, title, type, buildID) VALUES (@caption, @fileName, @orderNumber, @status, @thumbnailPath, @timeStamp, @title, @type, @buildID)";// "," + b.fileName + "," + b.orderNumber + "," + b.status + "," + b.thumbnailPath + "," + b.timeStamp + "," + b.title + "," + b.type + "," + b.buildID + ")"; SqlCommand cmdBuildItems = new SqlCommand(itemExists, buildItemsDB); // add the parameters cmdBuildItems.Parameters.Add("@caption", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@caption"].Value = b.caption; cmdBuildItems.Parameters.Add("@fileName", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@fileName"].Value = b.fileName; cmdBuildItems.Parameters.Add("@orderNumber", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@orderNumber"].Value = b.orderNumber; cmdBuildItems.Parameters.Add("@status", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@status"].Value = b.status; cmdBuildItems.Parameters.Add("@thumbnailPath", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@thumbnailPath"].Value = b.thumbnailPath; cmdBuildItems.Parameters.Add("@timeStamp", System.Data.SqlDbType.DateTime); cmdBuildItems.Parameters["@timeStamp"].Value = b.timeStamp; cmdBuildItems.Parameters.Add("@title", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@title"].Value = b.title; cmdBuildItems.Parameters.Add("@type", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@type"].Value = b.type; cmdBuildItems.Parameters.Add("@buildID", System.Data.SqlDbType.BigInt); cmdBuildItems.Parameters["@buildID"].Value = b.buildID; rdr = cmdBuildItems.ExecuteReader();// need to return the value we just created rdr.Close(); string selectLatestString = "SELECT buildItemID FROM BuildItems WHERE buildID=@buildID AND orderNumber=@orderNumber"; cmdBuildItems = new SqlCommand(selectLatestString, buildItemsDB); cmdBuildItems.Parameters.Add("@buildID", System.Data.SqlDbType.BigInt); cmdBuildItems.Parameters["@buildID"].Value = b.buildID; cmdBuildItems.Parameters.Add("@orderNumber", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@orderNumber"].Value = b.orderNumber; int returnBuildItemID = (int)cmdBuildItems.ExecuteScalar(); return "new buildItemID = " + Convert.ToString(returnBuildItemID); } catch (SqlException e) { return "failure adding record"; } finally { rdr.Close(); rdr = null; buildItemsDB.Close(); } return "record created"; }
private string updateBuildItems(BuildItem b) { SqlDataReader rdr = null;// setup the sql reader SqlConnection buildItemsDB = new SqlConnection();// get the conn buildItemsDB.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BuildsConnectionString"].ToString(); buildItemsDB.Open();// open the database try { // update the data for this item string updateData = "UPDATE BuildItems SET caption=@caption, filename=@fileName,orderNumber=@orderNumber,status=@status,thumbnailPath=@thumbnailPath,timeStamp=@timeStamp,title=@title,type=@type WHERE buildItemID=@buildItemID AND buildID=@buildID"; SqlCommand cmdBuildItems = new SqlCommand(updateData, buildItemsDB); // add the parameters cmdBuildItems.Parameters.Add("@caption", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@caption"].Value = b.caption; cmdBuildItems.Parameters.Add("@fileName", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@fileName"].Value = b.fileName; cmdBuildItems.Parameters.Add("@orderNumber", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@orderNumber"].Value = b.orderNumber; cmdBuildItems.Parameters.Add("@status", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@status"].Value = b.status; cmdBuildItems.Parameters.Add("@thumbnailPath", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@thumbnailPath"].Value = b.thumbnailPath; cmdBuildItems.Parameters.Add("@timeStamp", System.Data.SqlDbType.DateTime); cmdBuildItems.Parameters["@timeStamp"].Value = b.timeStamp; cmdBuildItems.Parameters.Add("@title", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@title"].Value = b.title; cmdBuildItems.Parameters.Add("@type", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@type"].Value = b.type; cmdBuildItems.Parameters.Add("@buildID", System.Data.SqlDbType.BigInt); cmdBuildItems.Parameters["@buildID"].Value = b.buildID; cmdBuildItems.Parameters.Add("@buildItemID", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@buildItemID"].Value = b.buildItemID; rdr = cmdBuildItems.ExecuteReader(); rdr.Close(); } catch (SqlException e) { return "failure adding record"; } finally { rdr.Close(); rdr = null; buildItemsDB.Close(); } return "buildItemID = " + Convert.ToString(b.buildItemID); }
public List<BuildItem> GetBuildItemsForId(int id) { List<BuildItem> items = new List<BuildItem>(); SqlDataReader rdr = null; SqlConnection buildItemsDB = new SqlConnection();// get the conn buildItemsDB.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BuildsConnectionString"].ToString(); buildItemsDB.Open(); // select if it has the right buildID and orderNumber string getBuild = String.Format("SELECT buildID FROM Builds WHERE applicationID = {0}", id); SqlCommand getBuildComm = new SqlCommand(getBuild, buildItemsDB); try { string testID = getBuildComm.ExecuteScalar().ToString(); if (testID != null) { string getItems = String.Format("SELECT * FROM BuildItems WHERE buildID LIKE '{0}'", testID); SqlCommand getItemsComm = new SqlCommand(getItems, buildItemsDB); rdr = getItemsComm.ExecuteReader(); if (rdr.HasRows) { while (rdr.Read()) { BuildItem b = new BuildItem(); b.status = Convert.ToString(rdr["status"]); b.caption = Convert.ToString(rdr["caption"]); b.orderNumber = Convert.ToInt32(rdr["orderNumber"]); b.thumbnailPath = Convert.ToString(rdr["thumbnailPath"]); b.title = Convert.ToString(rdr["title"]); b.type = Convert.ToString(rdr["type"]); b.buildID = Convert.ToString(rdr["buildID"]); b.timeStamp = Convert.ToDateTime(rdr["timeStamp"]); b.fileName = Convert.ToString(rdr["fileName"]); long bIID = (long)rdr["buildItemID"]; b.buildItemID = Convert.ToInt32(bIID); items.Add(b); } rdr.Close(); } } } catch (Exception e) { return null; } finally { rdr = null; buildItemsDB.Close(); } return items; }
public BuildItem GetBuildItem(int buildID, int orderNumber)// gets and returns the buildItem by finding through the order number and buildID { SqlDataReader rdr = null; SqlConnection buildItemsDB = new SqlConnection();// get the conn buildItemsDB.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BuildsConnectionString"].ToString(); buildItemsDB.Open(); // select if it has the right buildID and orderNumber string getItemCommStr = String.Format("SELECT * FROM BuildItems WHERE buildID = {0} AND orderNumber = {1}", buildID, orderNumber); SqlCommand getItemComm = new SqlCommand(getItemCommStr, buildItemsDB); try { rdr = getItemComm.ExecuteReader(); BuildItem returnItem = new BuildItem(); if (rdr.HasRows) { while (rdr.Read()) { returnItem.status = Convert.ToString(rdr["status"]); returnItem.caption = Convert.ToString(rdr["caption"]); returnItem.orderNumber = Convert.ToInt32(rdr["orderNumber"]); returnItem.thumbnailPath = Convert.ToString(rdr["thumbnailPath"]); returnItem.title = Convert.ToString(rdr["title"]); returnItem.type = Convert.ToString(rdr["type"]); returnItem.buildID = Convert.ToString(rdr["buildID"]); returnItem.timeStamp = Convert.ToDateTime(rdr["timeStamp"]); returnItem.fileName = Convert.ToString(rdr["fileName"]); long bIID = (long)rdr["buildItemID"]; returnItem.buildItemID = Convert.ToInt32(bIID); } } return returnItem; } catch (Exception e) { return null; } finally { rdr.Close(); rdr = null; buildItemsDB.Close(); } }
[NonAction]// inserts a new builditem and returns the integer of the new buildItem private int insertBuildItems(BuildItem b) { SqlDataReader rdr = null;// setup the sql reader SqlConnection buildItemsDB = new SqlConnection();// get the conn buildItemsDB.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BuildsConnectionString"].ToString(); buildItemsDB.Open();// open the database string[] keys = new string[10]; keys[0] = "title"; keys[1] = "caption"; keys[2] = "status"; keys[3] = "thumbnailPath"; keys[4] = "timeStamp"; keys[5] = "orderNumber"; keys[6] = "buildID"; keys[7] = "type"; keys[8] = "buildItemID"; keys[9] = "buildItemIDString"; string[] ss = new string[10]; ss[0] = b.title; ss[1] = b.caption; ss[2] = b.status; ss[3] = b.thumbnailPath; ss[4] = b.timeStamp.ToLongTimeString(); ss[5] = Convert.ToString(b.orderNumber); ss[6] = b.buildID; ss[7] = b.type; ss[8] = Convert.ToString(b.buildItemID); ss[9] = b.buildItemIDString; string logStr = ""; for (int i = 0; i < ss.Length; i++) { logStr += keys[i] + " : " + ss[i] + " "; } log(logStr); try { // check to see if it exists. If this one already exists, then update it with the new content, else insert it string itemExists = "INSERT INTO BuildItems (caption, fileName, orderNumber, status, thumbnailPath, timeStamp, title, type, buildID, buildItemIDString) VALUES (@caption, @fileName, @orderNumber, @status, @thumbnailPath, @timeStamp, @title, @type, @buildID, @buildItemIDString)";// "," + b.fileName + "," + b.orderNumber + "," + b.status + "," + b.thumbnailPath + "," + b.timeStamp + "," + b.title + "," + b.type + "," + b.buildID + ")"; SqlCommand cmdBuildItems = new SqlCommand(itemExists, buildItemsDB); // add the parameters cmdBuildItems.Parameters.Add("@caption", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@caption"].Value = b.caption; cmdBuildItems.Parameters.Add("@fileName", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@fileName"].Value = b.fileName; cmdBuildItems.Parameters.Add("@orderNumber", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@orderNumber"].Value = b.orderNumber; cmdBuildItems.Parameters.Add("@status", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@status"].Value = b.status; cmdBuildItems.Parameters.Add("@thumbnailPath", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@thumbnailPath"].Value = b.thumbnailPath; cmdBuildItems.Parameters.Add("@timeStamp", System.Data.SqlDbType.DateTime); cmdBuildItems.Parameters["@timeStamp"].Value = b.timeStamp; cmdBuildItems.Parameters.Add("@title", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@title"].Value = b.title; cmdBuildItems.Parameters.Add("@type", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@type"].Value = b.type; cmdBuildItems.Parameters.Add("@buildID", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@buildID"].Value = b.buildID; cmdBuildItems.Parameters.Add("@buildItemIDString", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@buildItemIDString"].Value = b.buildItemIDString; rdr = cmdBuildItems.ExecuteReader();// need to return the value we just created rdr.Close(); string selectLatestString = "SELECT buildItemID FROM BuildItems WHERE buildID=@buildID AND orderNumber=@orderNumber"; cmdBuildItems = new SqlCommand(selectLatestString, buildItemsDB); cmdBuildItems.Parameters.Add("@buildID", System.Data.SqlDbType.Char); cmdBuildItems.Parameters["@buildID"].Value = b.buildID; cmdBuildItems.Parameters.Add("@orderNumber", System.Data.SqlDbType.Int); cmdBuildItems.Parameters["@orderNumber"].Value = b.orderNumber; long returnID = 0; rdr = cmdBuildItems.ExecuteReader(); if (rdr.HasRows) { while (rdr.Read()) { // read the rows and return the ID } } rdr.Close(); return (int)returnID; } catch (SqlException e) { throw e; } finally { buildItemsDB.Close(); } }
public Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpRequestException("Not MultiPart");// pass on the exception, letting the catcher filter know that it's not multipart } string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); string medDir = HttpContext.Current.Server.MapPath("~/Media"); // Read the form data and return an async task. var task = Request.Content.ReadAsMultipartAsync(provider). ContinueWith<HttpResponseMessage>(t => { if (t.IsFaulted || t.IsCanceled) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, t.Exception); } try { BuildItem newB = new BuildItem(); newB.status = provider.FormData["status"]; newB.caption = provider.FormData["caption"]; newB.orderNumber = Convert.ToInt32(provider.FormData["orderNumber"]); newB.title = provider.FormData["title"]; newB.type = provider.FormData["type"]; newB.buildID = provider.FormData["buildID"]; newB.buildItemIDString = provider.FormData["buildItemIDString"]; newB.timeStamp = new DateTime(2013, 3, 21, 21, 34, 0); string ext = (newB.type == "image") ? ".jpg" : ".mp4"; string newFileName = Convert.ToString(newB.buildID) + "_" + Convert.ToString(newB.orderNumber) + ext; newB.fileName = newFileName; newB.thumbnailPath = String.Format("{0}_thumb.jpg", newB.buildID); int newBuildID = insertBuildItems(newB); //This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); string fileName = file.Headers.ContentDisposition.FileName; fileName = file.Headers.ContentDisposition.FileName;//System.Net.WebUtility.HtmlDecode(fileName); medDir = medDir + @"\"; string newPath = String.Format("{0}{1}", medDir, newFileName); //file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.LastIndexOf(".")),ext); Trace.WriteLine(newPath); // check if the file already exists and then delete it before moving if (File.Exists(newPath)) { File.Delete(newPath); } // move the file to the new location. File.Move(file.LocalFileName, newPath); //if (newB.orderNumber == 0) //{ // Bitmap newThumbImg = (newB.type == "image") ? buildThumb(newPath, newB.buildID) : null; // if(newB.type == "video") // GetVideoThumbnail(newPath, medDir); // if (newThumbImg != null) // { // ImageCodecInfo myImageCodecInfo; // Encoder myEncoder; // EncoderParameter myEncoderParameter; // EncoderParameters myEncoderParameters; // myImageCodecInfo = GetEncoderInfo("image/jpeg"); // myEncoder = Encoder.Quality; // myEncoderParameters = new EncoderParameters(1); // myEncoderParameter = new EncoderParameter(myEncoder, 75L); // myEncoderParameters.Param[0] = myEncoderParameter; // string newThumbPath = String.Format("{0}{1}", medDir, newB.thumbnailPath); // FileStream stream = new FileStream(newThumbPath, FileMode.Create); // newThumbImg.Save(stream, myImageCodecInfo, myEncoderParameters); // newThumbImg.Dispose(); // stream.Dispose();// release the stream // } //} } Dictionary<string, int> retDic = new Dictionary<string, int>(); retDic.Add("newBuildID", newBuildID); retDic.Add("orderNumber", newB.orderNumber); HttpResponseMessage hr = Request.CreateResponse(HttpStatusCode.OK, retDic); hr.ReasonPhrase = Convert.ToString(newBuildID); return hr; } catch (ArgumentNullException e) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Null object supplied by app" + e.Message, e); return r; } catch (ArgumentException a) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "failed to save " + a.Message, a); return r; } catch (NullReferenceException e) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Null object supplied by app" + e.Message, e); return r; } catch (FormatException e) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Invalid Format for Field" + e.Message, e); return r; } catch (InvalidCastException e) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid Cast" + e.Message, e); return r; } catch (SqlException sE) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "SQL Insert Failed " + sE.Message, sE); log(sE.Message); return r; } catch (Exception e) { HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "failed to save " + e.Message, e); return r; } finally { } }); return task; }