HRESULT         hr    = S_OK; hr=m_opConn.CreateInstance(/*"ADODB.Connection"*/__uuidof(Connection)); // 初始化一个连接
if( SUCCEEDED( hr ) )
{
}
else
return ; CString strDriver,strServerPath,strDb,strUid,strPwd;
strDriver = "MySQL ODBC 5.1 Driver";
strServerPath = "192.168.1.194";
strDb = "test";
strUid = "root";
strPwd = ""; CString strConnect; // 格式化连接
    strConnect.Format("Provider=MSDASQL.1;Driver={%s};Server=%s;DataBase=%s;UID=%s;PWD=%s;", 
      strDriver/*MYSQL驱动,如:"MySQL ODBC 3.51 Driver"*/, 
      strServerPath/*数据库路径,例子127.0.0.1*/, 
      strDb/*数据库名*/, 
      strUid/*用户名,默认为root*/, 
      strPwd/*密码*/); 
     hr = m_opConn->Open(_bstr_t(strConnect.GetBuffer(200)), "", "", adConnectUnspecified); // 打开连接
if( SUCCEEDED( hr ) )
{
MessageBox("成功!","Suc",MB_OK);
}
else
{
MessageBox("失败!","Suc",MB_OK);
}}void CSqlDlg::OnButton2() 
{
// TODO: Add your control notification handler code here
if(NULL == m_opConn )
return ;  opSet.CreateInstance(__uuidof(Recordset));

    CString strSQL;
    // tb_dzjc
    //    +---------------+---------------+
    //    |   Numb(char)   | Name(varchar) |
    //    +---------------+---------------+
    strSQL.Format("select *  from dzjc");
    _variant_t vtConn = _variant_t(m_opConn, true); opSet->CursorLocation = adUseClient;// 设定游标    opSet->Open(_variant_t((LPCTSTR)strSQL), vtConn, adOpenStatic, adLockOptimistic, adCmdText); // 执行SQL语句,打开记录集
    long lCount = opSet->GetRecordCount(); // 表中记录的数目
    opSet->MoveFirst(); // 到记录集的第一行
    for(int i = 0; i < lCount; i++)
    {
        _variant_t vt = opSet->GetCollect(_variant_t(_T("id")));
 
BOOL bRet = false;
int iId;
CString  strPath,strTemp,strId,strPic;
if( vt.vt == VT_BSTR )
{
strId=vt.bstrVal;
bRet = true;
}
else
if(vt.vt==VT_I4)
{
iId=vt.intVal;
bRet = true;}
vt = opSet->GetCollect(_variant_t(_T("path")));
if( vt.vt == VT_BSTR )
{
strPath=vt.bstrVal;
bRet = true;
}
 vt = opSet->GetCollect(_variant_t(_T("pic")));
  if( vt.vt == VT_BLOB_OBJECT )
{
strPic=vt.bstrVal;
bRet = true;
} if( bRet  )
{
strTemp.Format("ID:%d-PATH:%s-PIC:%s",iId,strPath,strPic);
m_ListCtrl.AddString(strTemp);
UpdateData(false);
}        opSet->MoveNext();
    }
    // 完事了,打扫战场
   // opSet->Close();
}void CSqlDlg::OnButton3() 
{
CFile imagefile;
CString  FilePathName;
  CFileDialog dlg(TRUE,NULL,NULL,0,"jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
 if(dlg.DoModal()==IDOK)
 FilePathName=dlg.GetPathName();
if(0 == imagefile.Open(FilePathName,CFile::modeRead))
return; opSet = NULL;
 m_opConn = NULL;
_variant_t varChunk;
HRESULT hr;
BYTE* pbuf;
long nLength = imagefile.GetLength();
pbuf = new BYTE[nLength+2];
if(pbuf == NULL)
return; //allocate memory error;
imagefile.Read(pbuf,nLength); //read the file into memory
BYTE *pBufEx;
pBufEx = pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
//_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");_bstr_t strCnn(strConnect.GetBuffer(200)); opSet->AddNew();opSet->Fields->GetItem("pic")->AppendChunk(varBLOB);opSet->Update();
opSet->Close();}