怎样将VC与SQL连接起来?同时,为了检验连接成功,怎样在VC中用较为简便的语句查出数据库中的信息?

解决方案 »

  1.   

    用什么方式联的阿,在程序中执行SQL语句不就行了
      

  2.   

    我也正在连着,sql连了半天也没连上,本来就是
    主要语句:不用密码和用户名
    1.bstr_t strConnect= "Provider=SQLOLEDB.1;Server=(local);Database=CJDB; uid=; pwd=";
    2.m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), "", "",adModeUnknown);
    现在我的1有问题,奶奶的,没用过sql还真麻烦,sql创建的数据库后缀名是什么,比如Access的是.mdb,sql呢?
      

  3.   

    ::1.bstr_t strConnect= "Provider=SQLOLEDB.1;Server=(local);Database=CJDB; uid=;   pwd=";用户名是必须的
    _bstr_t strConnect= "Provider=SQLOLEDB.1;user id=sa;pwd=;persist security info=false;initial catalog=CJDB;data source=(local);"
      

  4.   

    终于连接成功了,哎,废了好大的劲
    1.#import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename_namespace("ADOCG") rename("EOF", "EndOfFile")
    using namespace ADOCG;
    #include "icrsint.h"
    2.
    protected:
    _ConnectionPtr m_pConnection;                  
    _RecordsetPtr m_pRecordset;  
    3.   在构造函数里
    m_pConnection = NULL;
    m_pRecordset  = NULL;
    4               bstr_t strConnect= "Driver={SQL Server};Server=127.0.0.1;Database=CJDB; uid=sa; pwd=你的密码";
    //_bstr_t strConnect = "Provider=SQLOLEDB.1; Server=(local);Database=CJDB; uid=sa; pwd=456852;";
                          //打开数据库
    m_pConnection.CreateInstance(_T("ADODB.Connection"));//创建 Connection 对象 
    #ifdef _DEBUG
    if (m_pConnection == NULL)
    {
    AfxMessageBox(_T("Connection 对象创建失败! 请确认是否初始化了COM环境\r\n"));
    }
    #endif
    ASSERT(m_pConnection != NULL);
    HRESULT hr=m_pConnection.CreateInstance(_T("ADODB.Connection"));
    if(!hr)
    {
    try 
    {
    //m_pConnection->Open(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DBCJ.mdb"),"","",adModeUnknown);
    m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), "", "",adModeUnknown);
    AfxMessageBox("数据库Access2连接成功,恭喜");

    catch(_com_error e) 

    AfxMessageBox(_T("数据库连接失败,确认数据库DBCJ.mdb是否在当前路径下!")); 


    }
    else
    AfxMessageBox(_T("m_pConnection创建错误"));
    5.在西够函数里
    if(m_pConnection )
    m_pConnection->Close(); 
    m_pConnection = NULL;你要连接的话主要是这两句 
    本地的
    1.bstr_t strConnect= "Driver={SQL Server};Server=127.0.0.1;Database=CJDB; uid=sa; pwd=1213";
    2. m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), "", "",adModeUnknown);
      

  5.   

    4.//这样你看起来简单点,
    void CTest::OnButton1() 
    {
    bstr_t strConnect= "Driver={SQL Server};Server=127.0.0.1;Database=CJDB; uid=sa; pwd=456852";
    m_pConnection.CreateInstance(_T("ADODB.Connection"));//创建 Connection 对象 
    HRESULT hr=m_pConnection.CreateInstance(_T("ADODB.Connection"));
    if(!hr)
    {
    try 
    {
             m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), "", "",adModeUnknown);
    AfxMessageBox("数据库sql连接成功,恭喜");

    catch(_com_error e) 

    AfxMessageBox(_T("数据库连接失败,确认数据库DBCJ是否在当前路径下!")); 


    }
    else
    AfxMessageBox(_T("m_pConnection创建错误"));
    }