簡體   English   中英

SQL存儲過程到帶時間戳的csv文件

[英]Sql Stored Procedure to csv file with Timestamp

該應用程序將來自SQL存儲過程的結果存儲到給定的csv中。 文件必須在文件名中包含時間戳。 通過我的任何研究,我一直未能成功找到解決方案。 這是代碼,請記住,時間戳需要有日期,最重要的是時間'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();
                }
            }
        }

通過此實現集思廣益,我相信可以通過開始和結束時間字符串將其合並。

我仍在考慮這個問題的標題。

似乎要與原始CSV數據一起存儲一些元數據。 某些文件類型取決於文件類型,具有大量的元數據屬性,例如作者身份和公司名稱等。因此,在這種情況下,我可能會選擇使用令人驚嘆的ClosedXML庫以XSLX格式存儲CSV數據。 XSLX文件類型具有許多元數據屬性,可以存儲您的時間戳等等。

下面是將屬性添加到Docx文件的示例。 這僅表明Microsoft Office格式具有許多可以訪問和使用的可用元數據屬性。
如何設置擴展文件屬性?

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

認為它只是當前日期格式的一種簡單用法。

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

格式是您需要的格式-日期,時間,刻度等-您需要什么格式來獲取所需的粒度。 如果需要,您可以轉到formo字符串“ o”以將其降至十進制的亞秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM