使用RxADO封装对数据库的操作:
class RxADO
{
public:
void Close();
_ConnectionPtr cnn;
_ConnectionPtr GetConnection();
RxRecordset record;
CString AutoNumber(CString sTable, CString sFieldName, CString sCode, int nStyle=1);
int GetRecordCount(_RecordsetPtr pRst);
void GetADOErrors(_com_error eErrors);
CString FieldToOtherField(CString cDataBaseName, CString cFieldName, CString cValue, CString cReturnField, int nStale);
bool SetConnection(CString LinkString);
RxADO();
virtual ~RxADO();
};bool RxADO::SetConnection(CString LinkString)
{
::CoInitialize(NULL);
cnn=NULL;
cnn.CreateInstance(__uuidof(Connection));
cnn->ConnectionString=(_bstr_t)LinkString;
try{
cnn->Open(L"",L"",L"",adCmdUnspecified);
}
catch(_com_error& e)
{
ErrorsPtr pErrors=cnn->GetErrors();
if (pErrors->GetCount()==0)
{
CString ErrMsg=e.ErrorMessage();
MessageBox(NULL,"发生错误:\n\n"+ErrMsg,"系统提示",MB_OK|MB_ICONEXCLAMATION);
return false;
}
else
{
for (int i=0;i<pErrors->GetCount();i++)
{
_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
MessageBox(NULL,"发生错误:\n\n"+desc,"系统提示",MB_OK|MB_ICONEXCLAMATION);
return false;
}
}
}
return true;
}...
在应用程序的InitInstance()函数中设置连接,如下:BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
//载入动态库
hInstance =LoadLibrary("RxDLL.dll");
//载入RXDLL中的函数
MoneyToChineseCode=(_MoneyToChineseCode*)GetProcAddress(hInstance,"MoneyToChineseCode");
CTimeToCString=(_CTimeToCString*)GetProcAddress(hInstance,"CTimeToCString");
CStringTOCTime=(_CStringTOCTime*)GetProcAddress(hInstance,"CStringTOCTime");
Padl=(_Padl*)GetProcAddress(hInstance,"Padl");
CharToLetterCode=(_CharToLetterCode*)GetProcAddress(hInstance,"CharToLetterCode");
if(ado.SetConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=gdzc;Data Source=(local)")==false) return false;
cnn=ado.GetConnection();
...结果出现"系统提示",发生错误:用户‘XX\XX’登录失败。调试发现在红色语句处出现错误,请问这是怎么回事?
class RxADO
{
public:
void Close();
_ConnectionPtr cnn;
_ConnectionPtr GetConnection();
RxRecordset record;
CString AutoNumber(CString sTable, CString sFieldName, CString sCode, int nStyle=1);
int GetRecordCount(_RecordsetPtr pRst);
void GetADOErrors(_com_error eErrors);
CString FieldToOtherField(CString cDataBaseName, CString cFieldName, CString cValue, CString cReturnField, int nStale);
bool SetConnection(CString LinkString);
RxADO();
virtual ~RxADO();
};bool RxADO::SetConnection(CString LinkString)
{
::CoInitialize(NULL);
cnn=NULL;
cnn.CreateInstance(__uuidof(Connection));
cnn->ConnectionString=(_bstr_t)LinkString;
try{
cnn->Open(L"",L"",L"",adCmdUnspecified);
}
catch(_com_error& e)
{
ErrorsPtr pErrors=cnn->GetErrors();
if (pErrors->GetCount()==0)
{
CString ErrMsg=e.ErrorMessage();
MessageBox(NULL,"发生错误:\n\n"+ErrMsg,"系统提示",MB_OK|MB_ICONEXCLAMATION);
return false;
}
else
{
for (int i=0;i<pErrors->GetCount();i++)
{
_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
MessageBox(NULL,"发生错误:\n\n"+desc,"系统提示",MB_OK|MB_ICONEXCLAMATION);
return false;
}
}
}
return true;
}...
在应用程序的InitInstance()函数中设置连接,如下:BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
//载入动态库
hInstance =LoadLibrary("RxDLL.dll");
//载入RXDLL中的函数
MoneyToChineseCode=(_MoneyToChineseCode*)GetProcAddress(hInstance,"MoneyToChineseCode");
CTimeToCString=(_CTimeToCString*)GetProcAddress(hInstance,"CTimeToCString");
CStringTOCTime=(_CStringTOCTime*)GetProcAddress(hInstance,"CStringTOCTime");
Padl=(_Padl*)GetProcAddress(hInstance,"Padl");
CharToLetterCode=(_CharToLetterCode*)GetProcAddress(hInstance,"CharToLetterCode");
if(ado.SetConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=gdzc;Data Source=(local)")==false) return false;
cnn=ado.GetConnection();
...结果出现"系统提示",发生错误:用户‘XX\XX’登录失败。调试发现在红色语句处出现错误,请问这是怎么回事?
{
AfxEnableControlContainer();
//载入动态库
hInstance =LoadLibrary("RxDLL.dll");
//载入RXDLL中的函数
MoneyToChineseCode=(_MoneyToChineseCode*)GetProcAddress(hInstance,"MoneyToChineseCode");
CTimeToCString=(_CTimeToCString*)GetProcAddress(hInstance,"CTimeToCString");
CStringTOCTime=(_CStringTOCTime*)GetProcAddress(hInstance,"CStringTOCTime");
Padl=(_Padl*)GetProcAddress(hInstance,"Padl");
CharToLetterCode=(_CharToLetterCode*)GetProcAddress(hInstance,"CharToLetterCode");
if(ado.SetConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=gdzc;Data Source=(local)")==false) return false;
cnn=ado.GetConnection();
...
if(ado.SetConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=gdzc;Data Source=(local)")==false)[/color] return false;
cnn=ado.GetConnection()
http://www.connectionstrings.com/sqlserver
http://blog.csdn.net/zyq5945/archive/2010/05/13/5586423.aspx
Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=gdzc;Data Source=127.0.0.1