我是vc新手,不知道怎么样连接数据库,请你知道我,好么?

解决方案 »

  1.   

    这个是连接 SqlServer的
    BOOL CreateConnect(void)
    { CString m_sPassword=_T("");
    CString m_sUser=_T("anonymous");
    CString m_sServer=_T("ug00");
    //CString strConn="uid=anonymous;pwd=;driver={SQL Server};server=ug00;database=STDLIBRARY;dsn=''";
        CString strConn;
    //strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database='';dsn='SPL'",m_sPassword,m_sUser,m_sServer);
    //strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sPassword,m_sUser,m_sServer);
    //建立连接串
        strConn.Format("driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sServer);
        
    //CString strConn="Provider=SQLOLEDB;Persist Security Info=False;User ID="+g_Global.m_strUserName+";Initial Catalog=GLSTD_LIB;Data Source="+g_Global.m_strServerName+";Password="+g_Global.m_strPassword+";";//ZHANGSG+
    HRESULT hr;
    try{ /*----------------问题出在这里--------------*/
    hr=gl_pConnection.CreateInstance(__uuidof(Connection));

    if(SUCCEEDED(hr))
    {
    gl_pConnection->ConnectionTimeout=10;//连接时间
    hr=gl_pConnection->Open(_bstr_t((LPCTSTR)strConn),
    _bstr_t(L"anonymous"),
    _bstr_t(L""),
    adModeUnknown);
    if(SUCCEEDED(hr))
    {
    //g_Global.bConnected=TRUE;
    return TRUE;
    }
    }
    }
    catch(_com_error &e)//捕捉异常情况
    {
    #ifdef _DEBUG
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());


    CString csError;
    csError.Format(":\n\t编号:%08x\n\t编号含义:%s\n\t源代码:%s\n\t描述:%s\n",
    e.Error(),e.ErrorMessage(),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
    MessageBox(NULL,(LPCTSTR)csError,"[Arx->BuidDbConnection]引进的对象出现异常",MB_OK|MB_ICONEXCLAMATION);
    #endif
    //g_Global.bConnected=FALSE;
    return FALSE;
    }
    catch(...)
    {
    #ifdef _DEBUG
    AfxMessageBox("不可捕捉的异常。",MB_OK|MB_ICONEXCLAMATION);
    #endif
    //g_Global.bConnected=FALSE;
    return FALSE;
    }
        return FALSE; 
    }
      

  2.   

    你可以参考深入前出MFC和VC技术内幕两书中的数据库的编程实例。
      

  3.   

    看看这两篇文章,很好
    http://www.vckbase.com/document/viewdoc.asp?id=307
    http://www.vckbase.com/document/viewdoc.asp?id=215
      

  4.   

    先要#import ado15.dll
    建立_Connection
    初始化COM对象呵呵,只能这么提示了,你用Variant作为搜索关键字到MSDN里找,能找到相关内容的。用Ado作关键字找的东西太多了