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).
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.
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.
Ideas?
Updated #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)
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
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