title: Sql Stored Procedure to csv file with Timestamp
body: <p>The application stores results from a SQL stored procedure into a given csv. It is necessary for the file to have a timestamp within the filename. I haven't been successful finding the solution through any of my research. Here's the code, keep in mind that the timestamp needs to have the date and most importantly the time 'hh:ss'</p>
  
  <pre><code>string db = "databasename";
          string startTime = "2018-04-17 00:00:00.000";
          string endTime = "2018-04-17 23:59:59.997";
          string LiquorFile = "LiquorFile.csv";
  
          using (SqlConnection con = new SqlConnection(GlobalConfig.CnnString(db)))
          {
              var tableName = "liqTemp";
              var fileName = tableName + ".csv";
              var recordCount = 0;
              var fileCount = 0;
  
              SqlCommand scCmd = new SqlCommand("dbo.spGetInventory_Liquor", con);
              scCmd.CommandType = CommandType.StoredProcedure;
              SqlDataReader reader;
  
              con.Open();
  
              scCmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startTime;
              scCmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endTime;
  
              reader = scCmd.ExecuteReader();
  
              StreamWriter writer = null;
  
              try
              {
                  while (reader.Read())
                  {
                      if (writer == null || recordCount == 50000)
                      {
                          if (writer != null)
                          {
                              writer.Close();
                              writer.Dispose();
                          }
                          fileName = tableName + "_" + (++fileCount).ToString() + ".csv";
  
                          writer = new StreamWriter(fileName);
                      }
                      recordCount++;
                      writer.WriteLine("\t{0}\t{1}", reader.GetDecimal(0), reader.GetString(1));
                  }
                  reader.NextResult();
              }
              finally
              {
                  if (writer != null)
                  {
                      writer.Dispose();
                  }
              }
          }
  </code></pre>
  
  <p>Brainstorming through this implementation I believe this can be incorporated somehow through the start and end time string.  </p>
  
  <p>I'm still thinking of a proper title for this question. </p>