使用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’登录失败。调试发现在红色语句处出现错误,请问这是怎么回事?

解决方案 »

  1.   

    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();
    ...
      

  2.   

    不好意思,红色没显示,发生错误的语句是:
    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()
      

  3.   

    连接字符串不对
    http://www.connectionstrings.com/sqlserver
    http://blog.csdn.net/zyq5945/archive/2010/05/13/5586423.aspx
      

  4.   

    那应该怎么改啊。我的数据库的名称为gdzc,在安装sqlserver2000的时候在“服务设置”中选择的是“使用本地系统账户(L)”,身份验证模式选择的是“混合模式(Windows身份验证和SQL Server身份验证)”,添加的sa登录密码为空密码。在错误提示中“用户‘XX/YY’登录失败中”,XX是机器名,YY是登录系统时的用户名。这是怎么回事呢?究竟该怎么修改这个连接字符串啊,纠结!希望高手解答
      

  5.   

    第一篇文章好乱;第二篇文章我试着操作了,测试连接成功,然后按照 .udl文件里的内容修改连接字符串,还是出现同样的错误。究竟怎么回事啊
      

  6.   

    试试
    Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=gdzc;Data Source=127.0.0.1
      

  7.   

    谢谢,好使了!呵呵,看来是“Integrated Security=SSPI;”的原因