各位ADO编程高手,请教:
vc中读取SQL Server中的text类型的字段内容,采取什么方法最好?
我试过GetChunk, AppendChunk 方法,可是都不行,text类型的字段中写入.jpg, .rar文件, 用textcopy读出来时是正确的,可是vc读就没辙了,各位高手有高招否?
问题点数:100、回复次数:8Top
1 楼masterz(www.fruitfruit.com)回复于 2003-09-12 17:03:56 得分 90
主 题: 怎么样从数据库中取出二进制数据到ADO的Stream对象中来..
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
int main(int argc, char* argv[])
{
printf("This sample shows you how to access BLOB via ADO and store it as file"
"The second field of the database is BLOB\n"
"If it works, it is writen by masterz,\n"
"otherwise I don't know who write it\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
_variant_t varBLOB;
pConn->Open(_bstr_t("Driver={Microsoft Access Driver (*.mdb)};DBQ=GetChunk.mdb"),"","",adConnectUnspecified);
pRst->Open(_bstr_t("BlobTable"),_variant_t((IDispatch *) pConn, true),
adOpenKeyset, adLockOptimistic, adCmdTable);
pRst->MoveFirst();
//long lDataLength = pRst->Fields->Item[1L]->ActualSize;
//varBLOB = pRst->Fields->Item[1L]->GetChunk(lDataLength);
_StreamPtr stream;
stream.CreateInstance("ADODB.Stream");
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
stream->raw_Open(varOptional, adModeUnknown, adOpenStreamUnspecified,NULL,NULL);
stream->put_Type(adTypeBinary);
stream->Write(pRst->GetFields()->GetItem(_variant_t((long)1))->GetValue());
stream->SaveToFile("test.txt",adSaveCreateOverWrite);
printf("save stream to file finished\n");
stream->Close();
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}
Top
2 楼masterz(www.fruitfruit.com)回复于 2003-09-12 17:04:41 得分 5
///////////////////////////////////////////////////////////////////
//read "TEXT" field of SQL server table
#include "stdafx.h"
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" rename( "EOF", "adoEOF" )
struct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
void PrintProviderError(ADODB::_ConnectionPtr pConnection);
int main(int argc, char* argv[])
{
printf("read text field from SQL server\n by masterz\n");
ADODB::_ConnectionPtr Conn1;
ADODB::_RecordsetPtr Rs1;
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2 (DISP_E_PARAMNOTFOUND, VT_ERROR);
_bstr_t bstrConnect( L"driver={sql server};server=Cell;Database=zhg;UID=sa;PWD=;" );
try
{
_bstr_t bstrEmpty;
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Rs1.CreateInstance(__uuidof(ADODB::Recordset));
// Establish connection.
Conn1->ConnectionString = bstrConnect;
Conn1->Open( bstrConnect, bstrEmpty, bstrEmpty, -1 );
Rs1->put_CursorLocation(ADODB::adUseClient);
Rs1->Open("TABLE3", _variant_t((IDispatch *) Conn1, true),ADODB::adOpenStatic, ADODB::adLockReadOnly, ADODB::adCmdTable);
long ltextfield_index=3L;
long lDataLength = Rs1->Fields->Item[ltextfield_index]->ActualSize;
if(lDataLength)
{
VARIANT varBLOB;
VariantInit(&varBLOB);
// Get the chunk of data in the second field.
varBLOB = Rs1->Fields->Item[ltextfield_index]->GetChunk(lDataLength);
if(varBLOB.vt==VT_BSTR)
{
printf("Text field length:%d,valuse:%s\n",lDataLength,(LPCTSTR)_bstr_t(varBLOB.bstrVal));
}
else
printf("datalength:%d, not bstr value\n",lDataLength);
}
Rs1->Close();
Conn1->Close();
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nCOM error occurred, Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
PrintProviderError(Conn1);
}
return 0;
}
VOID PrintProviderError(ADODB::_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ADODB::ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;
if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description);
}
}
}
Top
3 楼bluebohe(薄荷)回复于 2003-09-12 17:05:30 得分 5
vc ADO读出来的text字段的var是_bstr_t类型的数据,里面存放的字符可能是UNICODE型的,所以你要准确的计算到地读出多少内容不大方便,但也不是没有办法,你可以把它强制转换成(LPCTSTR)后处理
比如m_pRecordSet->Fields->Item[strTabName]->ActualSize; 里面存放的是字符的长度
比如“a中国”长度是5,但是GetChunk(3)就能完全把他们取出来了
Top
4 楼wangjinwang(王者之疯)回复于 2003-09-12 17:13:16 得分 0
既然存储二进制文件,为什么不定义IMAGE类型字段呢。Top
5 楼yyfa6(yyfa6)回复于 2003-09-12 17:19:02 得分 0
高手们!Top
6 楼nirovf(司马缸-砸光 之 乡村程序员)回复于 2003-09-14 10:42:31 得分 0
正在学习中Top
7 楼shanjicn(有容乃大)回复于 2003-09-14 10:44:39 得分 0
学习Top
8 楼chen_pin(小品)回复于 2003-09-14 11:15:35 得分 0
UP ItTop




