简体   繁体   English

使用C#将计算机挂在500mb文件的批量插入中

[英]Computer hangs on bulk insert of a 500mb file with c#

I have text file of 550mb.When I run this code computer hangs after 10 mins and data insertion fails but this works fine for file of 100mb 我有550mb的文本文件。运行此代码时,计算机在10分钟后挂起,数据插入失败,但对于100mb的文件来说效果很好

 public static void BulkUpdate(string col, System.IO.StreamReader sr, string tableName)
    {
        ConnStr = ConfigurationSettings.AppSettings["ConnStrAffiliate"].Trim();
        SqlConnection myConn = DAO.fetchConnection();
 string[] value = col.Split(new char[] { '\t' },StringSplitOptions.RemoveEmptyEntries);                
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
            {
             dt.Columns.Add(new DataColumn(dc, typeof(string)));
}

        while (!sr.EndOfStream)
        {
            value = sr.ReadLine().Split(new char[] { '\t' });
            List<string> valueList = value.ToList();
            valueList.RemoveAt(valueList.Count - 1);
            value = valueList.ToArray();
            if (value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }
        SqlBulkCopy bc = new SqlBulkCopy(ConnStr, SqlBulkCopyOptions.TableLock);
        bc.DestinationTableName = tableName;
        bc.BatchSize = dt.Rows.Count;
        bc.WriteToServer(dt);
        bc.Close();
    }     

I suggest installing Glimpse ( http://getglimpse.com/ ) and then see what the webserver is doing. 我建议安装Glimpse( http://getglimpse.com/ ),然后查看Web服务器正在做什么。 My first hunch is that you are hitting a timeout value somewhere. 我的第一个直觉是您正在某个地方达到超时值。 You may also want to check the event logs to see if IIS logged an exception. 您可能还需要检查事件日志,以查看IIS是否记录了异常。

How much memory do you have available? 您有多少可用内存? You could break the file up into smaller parts first and load it into a staging table. 您可以先将文件分成较小的部分,然后将其加载到临时表中。

我不知道问题出在哪里,但是我尝试了Sql Server Integration服务来读取文件并将其上传到数据库中。

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

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