Last active
August 17, 2023 12:52
-
-
Save Vladimir-Novick/a827bdab95d5956b52f0b801b7a6ac99 to your computer and use it in GitHub Desktop.
CBlobDatabase- insert LogBynary object to SQL server database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "CBlobDatabase.h" | |
CBlobDatabase::CBlobDatabase() : CDatabase() { | |
m_nImageLen = 0; | |
m_pabImage = nullptr; | |
} | |
void CBlobDatabase::BindParameters(HSTMT hstmt) | |
{ | |
SQLINTEGER nLenOrInd = m_nImageLen; | |
if (m_sStmt.Find("?") == -1) { // no binding needed | |
return; | |
} | |
rc = SQLBindParameter( | |
hstmt, (SQLSMALLINT)1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARCHAR, | |
m_nImageLen, 0, (void*)m_pabImage, m_nImageLen, &nLenOrInd | |
); | |
// normally put error handling here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <afxdb.h> | |
/* | |
Vladimir Novick | |
Insert/Update SQL_C_BINARY object into SQL database | |
Example: | |
CBlobDatabase cDb; | |
cDb.OpenEx( your_connect_string_here ); | |
int nCntImgBytes= 5 * 1024 * 1024; | |
BYTE* pabData= new BYTE[nCntImgBytes]; // for test purposes, allocting 5MB buffer | |
for ( int j=0; j< 100; j++ ) { // make it recognizable | |
pabData[j]= j+'A'; | |
} | |
cDb.m_pabImage= pabData; | |
cDb.m_nImageLen= nCntImgBytes; | |
cDb.m_sStmt= "INSERT INTO PicRec (nIdx, sName, imgBinaryData) VALUES (123, 'TestImg', ?)"; | |
cDb.ExecuteSQL( cDb.m_sStmt ); | |
delete pabData; // delete test data | |
*/ | |
class CBlobDatabase : | |
public CDatabase | |
{ | |
public: | |
/// <summary> | |
/// Insert BLOB data to database | |
/// </summary> | |
CBlobDatabase(); | |
virtual void BindParameters(HSTMT hstmt); | |
BYTE* m_pabImage; // address of the data of the blob | |
int m_nImageLen; // sizeof the data in the image, in bytes | |
CString m_sStmt; // looks like "UPDATE MyTable SET myFld=? WHERE idxFld=123" | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment