16,371
社区成员




stdafx.h
#import "msado15.dll" no_namespace rename ("EOF", "adoEOF")
// 初始化数据库
BOOL Initialization(CString name , CString pass , CString ip , int port)
{
CString strport;
strport.Format(_T("%d"),port);
::CoInitialize(NULL);
try
{
if (!m_pConnection->State)
{
m_pConnection->Open(
_bstr_t(_T("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=iMsnData;Data Source="+ip+","+strport+";Network Library=DBMSSOCN"))
,_bstr_t(name), _bstr_t(pass), adOpenUnspecified);
}
}
catch(_com_error& e)
{
dump_com_error(e);
return FALSE;
}
return TRUE;
}
void dump_com_error(_com_error &e)
{
CString ErrorStr;
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
ErrorStr.Format(_T("\n\tADO Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n\n"),
e.Error(), e.ErrorMessage(), (LPCTSTR)bstrSource, (LPCTSTR)bstrDescription );
#ifdef _DEBUG
AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
#endif
}
// 读取数据库列名
CString GetField(CString strColumnName) //读列名
{
_variant_t var;
try
{
var = m_pRecordset->GetCollect((_variant_t)strColumnName);
if(var.vt != VT_NULL)
return (CString)(var);
else
return L"";
}
catch(_com_error& e)
{
//dump_com_error(e);
CString str;
str.Format(_T("%s"),e.ErrorMessage());
return (CString)(var = _T(""));
}
}
在工程的头文件声明变量
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;