Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在varbinary列中顺序写入blob_C#_Sql_Sql Server_Sequential_Varbinary - Fatal编程技术网

C# 在varbinary列中顺序写入blob

C# 在varbinary列中顺序写入blob,c#,sql,sql-server,sequential,varbinary,C#,Sql,Sql Server,Sequential,Varbinary,我必须将一些数据库还原到远程SQL Server。问题是,它们可能高达几gb,这将在一次读取所有字节时导致OutOfMemory异常。在较小的数据库上,它可以正常工作 现在,我尝试将数据库按顺序流式传输到SQL Server,如下所示: using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand()) { command.CommandText = "INSERT INTO " + dbName + ".dbo

我必须将一些数据库还原到远程SQL Server。问题是,它们可能高达几gb,这将在一次读取所有字节时导致OutOfMemory异常。在较小的数据库上,它可以正常工作

现在,我尝试将数据库按顺序流式传输到SQL Server,如下所示:

using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
{
    command.CommandText = "INSERT INTO " + dbName + ".dbo.tempTable (blob) values (@data)";
    command.CommandTimeout = 900;

    SqlParameter dataParam = new SqlParameter("@data", SqlDbType.VarBinary);
    command.Parameters.Add(dataParam);

    SqlParameter offsetParam = new SqlParameter("@offset", SqlDbType.BigInt);
    command.Parameters.Add(offsetParam);

    SqlParameter lengthParam = new SqlParameter("@len", SqlDbType.BigInt);
    command.Parameters.Add(lengthParam);

    using (FileStream fs = new FileStream(fileSource, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        byte[] buffer = new byte[8192];
        int read = 0;
        int offset = 0;

        while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
        {
            dataParam.Value = buffer;
            offsetParam.Value = offset;
            lengthParam.Value = read;

            command.ExecuteNonQuery();

            if (offset == 0)
            {
               command.CommandText = "UPDATE " + dbName + ".dbo.tempTable SET blob.WRITE(@data, @offset, @len)";
            }

            offset += read;
         }
     }
}
现在我的问题是:

500mb数据库需要15分钟以上的时间,然后会停止工作,但出现以下异常:

System.Data.Common.DbException:超时已过期。操作完成前已过超时时间,或者服务器没有响应

提前感谢,


放松

我怀疑超时是由于性能原因造成的。我猜没有任何数据支持,请注意,这是阻塞。某些东西阻止了查询的执行。阅读有关如何分析性能问题的提示,如您的问题、测量内容、查看内容等

请注意,您的更新。。。column.WRITE。。。是一个主要的候选人,确保你读了


作为旁注,我有一篇文章准确地描述了您的技术,包括对BLOB的阅读:。

我现在已经自己解决了这个问题。问题是我创建了一个8040字节的字节数组。这将是一个520mb的数据库,大约50000个请求。我现在已经将字节大小更改为8MB

byte[] buffer = new byte[8388608];

现在这很有效

顺便说一句,我不赞成将DB流式传输到BLOB列的想法,这是我对您所做工作的理解。使用文件传输协议。您可以举一个例子吗?我的公司有一些限制,但也许有更好的办法。