Last active
August 17, 2023 12:50
-
-
Save Vladimir-Novick/f61c19939872e97a25e7bb3aa624655e to your computer and use it in GitHub Desktop.
Read BLOB object from SQL 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 "CBlobRecordset.h" | |
CBlobRecordset::CBlobRecordset(CDatabase* db) : CRecordset(db) { | |
szErrorMessage[0] = 0; | |
nError = 0; | |
szSQLState[0] = 0; | |
} | |
long CBlobRecordset::GetBlobData(int nField, LPVOID pBuffer, int nBufferSize) | |
{ | |
SQLLEN nActualLength; | |
SQLRETURN r = ::SQLGetData(m_hstmt, (short)nField + 1, SQL_C_BINARY, pBuffer, nBufferSize, &nActualLength); | |
if (r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO) | |
{ | |
if (::SQLGetDiagRec(SQL_HANDLE_STMT, m_hstmt, 1, szSQLState, &nError, szErrorMessage, 100, NULL) == SQL_SUCCESS) | |
::AfxThrowDBException(r, m_pDatabase, m_hstmt); | |
} | |
return nActualLength; | |
} | |
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> | |
/* | |
Example: | |
Read varchar(max) Data: | |
CDatabase db; | |
// , DATALENGTH(Document) | |
CString strCon = _T("Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-RQTGTIQ\\SQLEXPRESS;Database=test2;Uid=sa;Pwd=XXXX;"); | |
CString strSQL = _T("SELECT DATALENGTH(Document) ,[Document] FROM [myTable] where FileName='ConnectionManager.pdb'"); | |
db.OpenEx(strCon); | |
CBlobRecordset rs(&db); | |
rs.Open(CRecordset::forwardOnly, strSQL); | |
CString strMessage ; | |
int len; | |
LPVOID blobBuffer ; | |
while (!rs.IsEOF()) | |
{ | |
rs.GetFieldValue((short)0, strMessage); | |
int fieldLen = atoi(strMessage); | |
blobBuffer = malloc(fieldLen+1); | |
long datalenn = rs.GetBlobData(1, blobBuffer, fieldLen+1); | |
rs.MoveNext(); | |
free(blobBuffer); | |
} | |
rs.Close(); | |
db.Close(); | |
*/ | |
#ifdef CONNECTIONMANAGER_EXPORTS | |
#define LIB_API_EXPORT __declspec(dllexport) | |
#else | |
#define LIB_API_EXPORT __declspec(dllimport) | |
#endif | |
class LIB_API_EXPORT CBlobRecordset : public CRecordset | |
{ | |
public: | |
CBlobRecordset(CDatabase *db = NULL); | |
/// <summary> | |
/// Read varchar(max) field | |
/// </summary> | |
/// <param name="nFieldID"></param> | |
/// <param name="pBuffer"></param> | |
/// <param name="nBufferSize"></param> | |
/// <returns></returns> | |
long GetBlobData(int nFieldID, LPVOID pBuffer, int nBufferSize); | |
/// <summary> | |
/// Error message ( ReadBLOBData ) | |
/// </summary> | |
SQLCHAR szErrorMessage[100]; | |
/// <summary> | |
/// Error number ( ReadBLOBData ) | |
/// </summary> | |
SQLINTEGER nError; | |
/// <summary> | |
/// five-character SQLSTATE code (and terminating NULL) for the diagnostic ( ReadBLOBData ) | |
/// </summary> | |
SQLCHAR szSQLState[10]; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment