当添加到sql服务器数据库时,删除一个字节[]

时间:2022-01-09 15:54:05

I have a byte array that will always be 10 megs. No matter what data is in it, it will be ten megs. The purpose has to do with the Large Object Heap (more information).

我有一个字节数组,它总是10mb。不管里面有什么数据,都是10兆。其目的与大型对象堆(更多信息)有关。

So if a 1Mb file is placed into the byte[], then the last nine megs are zero'd out. Problem is, when I upload this to SQL Server, the uploaded file is always 10 megs. I'd much rather upload only the necessary bits.

因此,如果一个1Mb的文件被放置到字节[]中,那么最后的9个megs就是0。问题是,当我上传这个到SQL Server时,上传的文件总是10兆。我宁愿只上传必要的数据。

I have the size before it's copied into the byte array, so I could do a trim if needs be. Problem is, I don't know how to do one efficiently that is not creating new byte[] on the LOH.

在将它复制到字节数组之前,我已经有了大小,所以如果需要的话,我可以修剪一下。问题是,我不知道如何在LOH上创建新的字节[]。

Ideas?

想法吗?

Updated #1

更新# 1

Here is some pseudo code. I've removed most of the unneccessary code. It's using the Microsoft.Practices.EnterpriseLibrary.Data library to access the database. (Legacy code, can't change)

这是一些伪代码。我去掉了大部分不必要的代码。这是使用Microsoft.Practices.EnterpriseLibrary。数据库访问数据库。(遗留代码,不能改变)

Param[] parametersArray = new Param[10];
// other params
parametersArray[4] = new Param("DocumentData", DbType.Binary, documentToSave.Data);
// other params
DataAccess.ExecuteNonQuery("esMD.proc_WS_UpdateDocument", parametersArray);

public static int ExecuteNonQuery(string spName, params Param[] parameters)
{
    int ret = -1;

    DbCommand storedProcedure = Database.Db.GetStoredProcCommand(spName);
    storedProcedure.CommandTimeout = commandTimeout;

    if (parameters != null)
    {
        foreach (Param parameter in parameters)
        {
            if (parameter != null)
            {
                Database.Db.AddInParameter(storedProcedure, parameter.ParameterName, parameter.DbType, parameter.Value);
            }
        }
    }

    try
    {
        ret = MedicareDatabase.Db.ExecuteNonQuery(storedProcedure);
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (storedProcedure != null)
        {
            storedProcedure.Dispose();
        }
    }
    return ret;
}

Updated #2

更新# 2

I modified the database call above and also modified how I entered Parameters.

我修改了上面的数据库调用,并修改了如何输入参数。

if (parameter.Size != null && parameter.Size > 0)
{
    MedicareDatabase.Db.AddParameter(storedProcedure, parameter.ParameterName, DbType.Binary, parameter.Size, ParameterDirection.Input, true, 0, 0, string.Empty, DataRowVersion.Default, parameter.Value); 
}
else
{
    MedicareDatabase.Db.AddInParameter(storedProcedure, parameter.ParameterName, parameter.DbType, parameter.Value);
}

This seems to be working for me. Does anyone see any issues with this?

这似乎对我有用。有人发现这有什么问题吗?

-Chad

乍得

1 个解决方案

#1


4  

Create a MemoryStream from the byte[] and desired length. Use the stream to construct a SqlBytes instance. Build the parameter using the SqlBytes.

从字节[]和所需的长度创建一个MemoryStream。使用流构造SqlBytes实例。使用SqlBytes构建参数。

As a side comment, your large data should never be materialized as a byte[]. It should live as a stream through the processing. See Download and Upload images from SQL Server via ASP.Net MVC for an actual example of how you can keep a large object as a stream, never materialized into a large byte[], from end-to-end on both download and upload paths.

作为旁注,您的大数据永远不应该以字节[]表示。它应该作为一条流通过处理。请参阅通过ASP从SQL Server下载和上传图片。Net MVC提供了一个实例,说明如何在下载和上传路径上保持一个大对象为流,而不会变成一个大字节[]。

#1


4  

Create a MemoryStream from the byte[] and desired length. Use the stream to construct a SqlBytes instance. Build the parameter using the SqlBytes.

从字节[]和所需的长度创建一个MemoryStream。使用流构造SqlBytes实例。使用SqlBytes构建参数。

As a side comment, your large data should never be materialized as a byte[]. It should live as a stream through the processing. See Download and Upload images from SQL Server via ASP.Net MVC for an actual example of how you can keep a large object as a stream, never materialized into a large byte[], from end-to-end on both download and upload paths.

作为旁注,您的大数据永远不应该以字节[]表示。它应该作为一条流通过处理。请参阅通过ASP从SQL Server下载和上传图片。Net MVC提供了一个实例,说明如何在下载和上传路径上保持一个大对象为流,而不会变成一个大字节[]。