简体   繁体   English

SQL存储过程到带时间戳的csv文件

[英]Sql Stored Procedure to csv file with Timestamp

The application stores results from a SQL stored procedure into a given csv. 该应用程序将来自SQL存储过程的结果存储到给定的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' 这是代码,请记住,时间戳需要有日期,最重要的是时间'hh:ss'

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();
                }
            }
        }

Brainstorming through this implementation I believe this can be incorporated somehow through the start and end time string. 通过此实现集思广益,我相信可以通过开始和结束时间字符串将其合并。

I'm still thinking of a proper title for this question. 我仍在考虑这个问题的标题。

Appears that you want to store some metadata along with the raw CSV data. 似乎要与原始CSV数据一起存储一些元数据。 Some file types, depending on the file type, have loads of metadata properties like authorship and company name etc. So, in this situation, I might elect to store my CSV data in XSLX format using the amazing ClosedXML library. 某些文件类型取决于文件类型,具有大量的元数据属性,例如作者身份和公司名称等。因此,在这种情况下,我可能会选择使用令人惊叹的ClosedXML库以XSLX格式存储CSV数据。 The XSLX file type has lots of metadata properties to store your timestamps and many more. XSLX文件类型具有许多元数据属性,可以存储您的时间戳等等。

Below is an example adding properties to a Docx file. 下面是将属性添加到Docx文件的示例。 This just shows that Microsoft office formats have lots of available metadata properties you can access and use. 这仅表明Microsoft Office格式具有许多可以访问和使用的可用元数据属性。
How to set extended file properties? 如何设置扩展文件属性?

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

string filePath = @"C:\temp\example.docx";
var file = ShellFile.FromFilePath(filePath);

// Read and Write:

string[] oldAuthors = file.Properties.System.Author.Value;
string oldTitle = file.Properties.System.Title.Value;

file.Properties.System.Author.Value = new string[] { "Author #1", "Author #2" };
file.Properties.System.Title.Value = "Example Title";

// Alternate way to Write:

ShellPropertyWriter propertyWriter =  file.Properties.GetPropertyWriter();
propertyWriter.WriteProperty(SystemProperties.System.Author, new string[] { "Author" });
propertyWriter.Close();

Think its just a simple use of current date formatting. 认为它只是当前日期格式的一种简单用法。

fileName = tableName + "_" + DateTime.Today.ToString("yyyyMMddHHmmss") + ".csv";

where the format is whatever you need - date, time, ticks, etc - whatever you need to get the granularity you desire. 格式是您需要的格式-日期,时间,刻度等-您需要什么格式来获取所需的粒度。 You can go to a formt string of "o" to get it down to decimal sub-seconds if you need to. 如果需要,您可以转到formo字符串“ o”以将其降至十进制的亚秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM