连接数据库的代码,楼主根据自己的情况改一下
try
{
if(FAILED(pConn.CreateInstance(_uuidof(Connection))))
{
AfxMessageBox("创建连接失败!");
return;
}
if(FAILED(pRS.CreateInstance(_uuidof(Recordset))))
{
AfxMessageBox("打开记录失败!");
return;
}
pConn->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=testdb;Data Source=COMPUTER4;","","",adModeUnknown); 
}//end of try
catch(_com_error &e)
{
CString msg;
msg="Error Description=";
msg+=e.Description();
AfxMessageBox(msg,MB_OK|MB_ICONEXCLAMATION,0);
}

解决方案 »

  1.   

    (2)好象没什么错啊,是不是m_Database类型不对啊?
      

  2.   

    我贴一段用ADO连SQL SEVER 的代码,保证可行,楼主可以模仿这个去修改。
    在头文件里加#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
    代码里这样写:
    CoInitialize(NULL);
    _ConnectionPtr pConn(__uuidof(Connection));
    _RecordsetPtr pRst(__uuidof(Recordset));
    // _CommandPtr pCmd(__uuidof(Command)); pConn->ConnectionString="Provider=SQLOLEDB.1;Data Source=132.155.11.15;User ID=sa;Password=11111;Initial Catalog=pubs";
        try{
    pConn->Open("","","",adConnectUnspecified);
    }
    catch(_com_error e){
        AfxMessageBox(e.ErrorMessage());
    }
    // pRst=pConn->Execute("select * from authors",NULL,adCmdText);
    pRst->Open("select * from authors",_variant_t((IDispatch*)pConn),
    adOpenDynamic,adLockOptimistic,adCmdText);
    // pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
    // pCmd->CommandText="select * from authors";
    pRst=pCmd->Execute(NULL,NULL,adCmdText);
    while(!pRst->rsEOF)
    {
    ((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
    (_bstr_t)pRst->GetCollect("au_lname"));
    pRst->MoveNext();
    }if(pConn!=NULL){
    pRst->Close(); pConn->Close();
    // pCmd.Release();
    pRst.Release();
    pConn.Release();
    }
    CoUninitialize();
      

  3.   

    http://blog.csdn.net/handsomerun/archive/2006/08/12/1055311.aspx
    看一下vckbase上的例子http://www.vckbase.com/document/viewdoc/?id=215这些都是ado的资料
      

  4.   

    下面程序运行后提示“SQL server第一行‘select* fromshopWhere'附近语法错误“,提示的很清楚了,你的sql语句不对啊空格,注意空格CString strSQL="Select* From  "+m_Database+"  Where AccountNo Like'";碰到这种sql语句不对的地方,可以调试一下,看最后你拼出来的sql语句是什么样的看看是否正确
      

  5.   

    ‘select* ' 應該是‘select * '吧?
      

  6.   

    m_database我是是如此定义的
    m_Database = _T("TableAccount");
    有错吗?
    “碰到这种sql语句不对的地方,可以调试一下,看最后你拼出来的sql语句是什么样的”
    请问一下这个sql应该怎么调试出来呢?
      

  7.   

    m_Database =_T("Shop");   
    m_Dsn =_T("TableAccount");CString strSQL="Select * From "+m_Database+" Where AccountNo Like'";
    程序代码部分如上,为什么现在出现的是“[SQL Server]对象名'Shop'无效呢?
      

  8.   

    _ConnectionPtr没有创建连接实例_RecordsetPtr中SQL语句不正确,注意空格
      

  9.   

    CString strSQL="Select* From"+m_Database+"Where AccountNo Like'";
    strSQL+=m_Username;
    strSQL+="'And Pswd Like'";
    strSQL+=m_Pswd;
    strSQL+="'";
    -----------
    上面这一堆,用strSQL.Format(...)一句话就搞定了……
    你的sql是不是缺少了哪个空格,上面代码看不出……
      

  10.   

    void CLoginDlg::OnOK() 
    {
    this->UpdateData(TRUE);


    HRESULT hr;
    _ConnectionPtr pConn;
    _CommandPtr pCommand; 
        try
    {
    /* pConn->Open("Provider=SQLOLEDB.1;Server=(local);DATABASE=m_Database;UID="";PWD=;","","",adModeUnknown);
    */
    _bstr_t Conn="DSN="+_bstr_t(m_Dsn)+";";
    pConn.CreateInstance(_uuidof(Connection));
        hr=pConn->Open(Conn,_bstr_t(""),_bstr_t(""),adModeUnknown);
         
    }
        catch(_com_error& ex)
    {
    AfxMessageBox(ex.Description());
    }
    try
    {
    CString strSQL="Select * From "+m_Database+" Where AccountNo Like'";
    strSQL+=m_Username;
    strSQL+="'And Pswd Like'";
    strSQL+=m_Pswd;
    strSQL+="'";
       /*   strSQL.Format("Select* From %s",m_Database);*/
    pCommand.CreateInstance(_uuidof(Command));
    pCommand->ActiveConnection=pConn;
    pCommand->CommandText=_bstr_t(strSQL);

    _RecordsetPtr pRecordset;
         pRecordset.CreateInstance(_uuidof(Recordset));
    pRecordset->CursorLocation=adUseClient; hr=pRecordset->Open((IDispatch*) pCommand,vtMissing,adOpenStatic,
    adLockOptimistic,adCmdUnknown);
    /* hr=pRecordset->Open(_bstr_t(strSQL),pConn.GetInterfacePtr(),adOpenStatic,
    adLockOptimistic,adCmdUnknown);*/
    if(pRecordset->End)
    {
    AfxMessageBox("用户或密码不正确!");
    pConn.Release(); IfRegisted=FALSE;
    } else 
    IfRegisted=TRUE;
        pConn.Release();
    }
    catch(_com_error& ex)
    {
    AfxMessageBox(ex.Description());
    } CDialog::OnOK();
    }
    整个函数的代码就是这样,我还有别的例子的也还是不行,晕死!
    就像下面这个程序,也是不行,怎么会不行呢,明明没错嘛
    #include"iostream.h"
    # import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile")
    int main(int argc,char* argv[])
    {
    CoInitialize(NULL);
    _ConnectionPtr m_pConnection("ADODB.Connection");
    _RecordsetPtr m_pRecordset("ADODB.Connection");
    try
    {        
     m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Demo.mdb;Persist Security Info=FALSE","","",adConnectUnspecified);
    }
    catch(_com_error &e)
    {
    cout<<e.ErrorMessage()<<endl;
    }

    try
    {
          m_pRecordset->Open("SELECT * FROM UserInfo",_variant_t(m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
              while(!m_pRecordset->End)
      {
         cout<<"UserName"<<_bstr_t(m_pRecordset->GetCollect("UserName"))<<"\t";
             cout<<"UserName"<<_bstr_t(m_pRecordset->GetCollect("Password"))<<endl;
            m_pRecordset->MoveNext();
      }
      m_pRecordset->Close();
          m_pConnection->Close();
    }
    catch(_com_error &e)
    {
    cout<<e.ErrorMessage()<<endl;

    return 0;
    }
      

  11.   

    _ConnectionPtr m_pConnection=("ADODB.Connection");
    _RecordsetPtr m_pRecordset("ADODB.Connection");
    这是???
    _ConnectionPtr m_pConnection=NULL;
    m_pConnection.CreateInstance(__uuidof(Connection));
    _RecordsetPtr m_pRecordset=NULL;
    m_pRecordset.CreateInstance(__uuidof(Recordset));
      

  12.   

    楼主去下点儿ADO的代码,自己改改就好了塞。你的代码好像没什么问题,是不是设置哪里有不对。
      

  13.   

    IADORecordBinding   *picRs = NULL;
    _RecordsetPtr pRs("ADODB.Recordset");
    _ConnectionPtr pConn("ADODB.Connection" );
    //CFootballTeamRs rsFootballTeam;
    GetModuleFileName(NULL,FileName,MAX_PATH);
    (_tcsrchr(FileName,'\\'))[1] = 0; lstrcat(FileName,_T("pass2.mdb")); strSql = strSql +
         "Provider=Microsoft.Jet.OLEDB.4.0;" +
     "Data Source = " + 
     FileName +
     ";Persist Security Info=False;Jet OLEDB:Database Password=cd;"; //pConn->ConnectionString = chDataSource;
    //pConn->Provider = _T("Microsoft.Jet.OLEDB.4.0");
    CString sqlSentence = _T("select * from pass");
    try
    {
    pConn->Open((_bstr_t)strSql, "", "", adModeUnknown);  pRs->QueryInterface(
      __uuidof(IADORecordBinding), (LPVOID*)&picRs);   pRs->Open(  (_variant_t)sqlSentence,                // 查询DemoTable表中所有字段
    pConn.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
    }
    catch(...)
    {} try
    {
    while(!(pRs->EndOfFile))
    {
    CString pass = (LPCTSTR)(_bstr_t)pRs->GetCollect("pass");
    if(pass.IsEmpty()) return;
    SetDlgItemText(IDC_PASS,pass);
    pRs->MoveNext();
    //break;
    }
    }
    catch(...)
    {}