使用C#将数据从块文件/批量导入到sql表中

时间:2016-10-18 12:12:44

标签: sql-server loading chunks

我想从包含大量数据的FLAT FILE(在TERABYTES中)将数据块/批量数据加载到SQL表中。

我想将数据块(例如1-10000,10001-20000,(...))加载到临时表中,因为临时表的内存有限。 将数据加载到临时表后,我想将数据加载到最终表中。

有人可以提供一些C#脚本,我可以将 FROM ID 号码提供给 TO ID 号码作为变量吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我过去做过类似的事情。这是粗略的语法。这是为Sql to Sql。请参阅底部的链接,了解如何使用ODBC来查询文本文件。

        int Start = 1;
        int End = 10000;
        using (SqlConnection SqlCon = new SqlConnection())
        {
            string Sql = $"Select * From Table Where Id Between {Start} and {End}";
            SqlCommand SqlCmd = new SqlCommand(Sql);
            SqlDataReader reader = SqlCmd.ExecuteReader();

            SqlBulkCopy bulk = new SqlBulkCopy(SqlCon);
            bulk.DestinationTableName = "StagingTable";
            bulk.WriteToServer(reader);
        }

http://www.c-sharpcorner.com/article/accessing-text-files-using-odbc-data-provider/