消防电话故障怎么处理:请问专家一个关于SQL的难题

来源:百度文库 编辑:高考问答 时间:2024/04/28 12:46:55
我要在VC环境下存储图片到SQL Server 数据库.有以下的代码,不知道为什么最后一个 pCommand->Execute(NULL,NULL,0);为什么会出错.
//实现将图片保存到数据库
//连接数据库
::CoInitialize(NULL);
_ConnectionPtr m_pConnection(__uuidof(Connection));
_bstr_t connectionstring = "Provider=sqloledb;Data Source=";
connectionstring += _T("(LOCAL)");
connectionstring += ";Initial Catalog=";
connectionstring += "image";
connectionstring += ";User Id=sa";
connectionstring += ";Password=";
connectionstring += ";";
_bstr_t userID = "sa";
_bstr_t password = "";
m_pConnection->Open(connectionstring, userID, password, 0);

_CommandPtr pCommand(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;
pCommand->CommandType = adCmdStoredProc;

_bstr_t imageTitle = "Image Title goes here";
_ParameterPtr titleParam = pCommand->CreateParameter(
"@imageTitle", adVarWChar,
adParamInput, 50, imageTitle);
pCommand->Parameters->Append(titleParam);
//读取文件
char *imageFileName = "D:\\打电话.bmp";
HANDLE hImageFile = CreateFile(imageFileName, GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
int imageLength = GetFileSize(hImageFile, NULL);

SAFEARRAY *imageBytesArray = SafeArrayCreateVector(VT_UI1, 0, imageLength);
void *pv;
SafeArrayAccessData(imageBytesArray, &pv);
DWORD bytesRead;
ReadFile(hImageFile, pv, imageLength, &bytesRead, NULL);
SafeArrayUnaccessData(imageBytesArray);
CloseHandle(hImageFile);
//将文件保存在数组中
VARIANT imageBytes;
imageBytes.vt = VT_ARRAY | VT_UI1;
imageBytes.parray = imageBytesArray;
_ParameterPtr imageBytesParam = pCommand->CreateParameter(
"@imageBytes", adLongVarBinary,
adParamInput, imageLength, imageBytes);
//添加到 pCommand
pCommand->Parameters->Append(imageBytesParam);
//数据库名是image,表名是image,字段是图片。使用的数据库是SQL Server2000
pCommand->Execute(NULL,NULL,0); //执行什么?不知道,怎么存到数据库呢。
m_pConnection->Close();

CommandText