Inserting large files in SQLServer
Posted on 2001-08-27
I am having trouble inserting large binary files in SQLServer. From what I can tell, my implementation is more or less an exact copy of the code samples in MSDN. The code works excellent for small files, but when they reach say 15 Mb in size SQLServer starts consuming memory rapidly (something like 500 Mb of RAM for a 15 Mb file).
I have tried running the client application both on the same computer as the database as well as on a separate computer and I have tried both SQLServer 7.0 and SQLServer 2000 on Windows NT4 and Windows 2000 always with the same result.
The database element is of type "image".
I would really appreciate if some one could help me with this. It does not matter if I have to use a totally different method for inserting the file as long as it does the job!
bool CDBInsertFile::AddBinary(ULONG rowid,const char* binaryFile)
bool rc = false;
if(binaryFile == NULL || *binaryFile == 0 || !VerifyAttachment(binaryFile))
rc = true;
else if(rowid != 0 && AllocStatement())
statement.Format("UPDATE t_file SET c_binary = ? WHERE c_rowid = %d",rowid);
if(Check(SQLPrepare(m_hstmt,(SQLCHAR *)(const char *)statement,SQL_NTS)))
SQLRETURN sql_rc = SQLExecute(m_hstmt);
while(sql_rc == SQL_NEED_DATA)
if((sql_rc = SQLParamData(m_hstmt,&pToken)) == SQL_NEED_DATA && (int(pToken) == 1))
FILE* fp = fopen(binaryFile,"rb");
while((len = fread(buf,1,1024,fp)) > 0)
rc = true;