//-------------------------------------------------------------------- // Build the Insert command String(commentFile) //-------------------------------------------------------------------- private String BuildInsertCommand(CommentFile CommentFile) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', {1})", CommentFile.FilePath1, CommentFile.CommentID.ToString()); String prefix = "INSERT INTO CommentFiles " + "(FilePath, commentID) "; command = prefix + sb.ToString(); return(command); }
//-------------------------------------------------------------------------------------------------- // This method inserts a commentFile to the commentFiles table //-------------------------------------------------------------------------------------------------- public int insert(CommentFile CommentFile) { SqlConnection con; SqlCommand cmd; try { con = connect("DBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(CommentFile); // helper method to build the insert string cmd = CreateCommand(cStr, con); // create the command try { int numEffected = cmd.ExecuteNonQuery(); // execute the command return(numEffected); } catch (Exception ex) { return(0); // write to log throw (ex); } finally { if (con != null) { // close the db connection con.Close(); } } }
//--------------------------------------------------------------------------------- // Read Comments Files list //--------------------------------------------------------------------------------- public List <CommentFile> getCFilesList() { List <CommentFile> commentsFilesList = new List <CommentFile>(); SqlConnection con = null; // classEX enter your code here try { con = connect("DBConnectionString"); // create a connection to the database using the connection String defined in the web config file String selectSTR = "SELECT * FROM CommentFiles"; SqlCommand cmd = new SqlCommand(selectSTR, con); // get a reader SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); // CommandBehavior.CloseConnection: the connection will be closed after reading has reached the end while (dr.Read()) { // Read till the end of the data into a row CommentFile cf = new CommentFile(); cf.FilePath1 = (string)dr["FilePath"]; cf.CommentID = Convert.ToInt32(dr["commentID"]); commentsFilesList.Add(cf); } return(commentsFilesList); } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { con.Close(); } } }
public void Post([FromBody] CommentFile[] arr) { CommentFile cf = new CommentFile(); cf.insert(arr); }
public ExtendedComment(CommentFile comment) { _comment = comment; }