请问 mfc 中如何 连接SQL SERVER2000
能不能给出具体步骤和代码
谢谢

解决方案 »

  1.   

    用ADO吧
    连接字符串为:
    Provider=SQLOLEDB.1; Data Source=ip地址或机器名;Initial Catalog=数据库名;User ID=用户名;Password=密码;
      

  2.   

    ODBC的:
    #include <afxdb.h>
    CDatabase db;
    BOOL OpenSQLServer(const CString &strServer, const CString &strDatabase, BOOL bWindowLink, const CString &strUID, const CString &strPWD)
    {
    BOOL bRet = FALSE;
    try
    {
    CString strODBC;
    if(bWindowLink)
    {
    //windows信任连接, 要求SQLServer支持, 并且本计算机已经与服务器建立了windows连接.
    strODBC.Format("Driver={SQL Server};Server=%s;Database=%s;Trusted_Connection=yes;",
    strServer,
    strDatabase);
    }
    else
    {
    strODBC.Format("Driver={SQL Server};Server=%s;Database=%s;Uid=%s;Pwd=%s",
    strServer,
    strDatabase,
    strUID,
    strPWD);
    } bRet = db.OpenEx(strODBC, CDatabase::noOdbcDialog );
    return bRet;

    }
    catch (CDBException &e)
    {
    TRACE("%s\n", e.m_strError);
    return FALSE;
    }
    catch (CMemoryException  &e)
    {
    TCHAR   szCause[250];
    CString strFormatted; e.GetErrorMessage(szCause, sizeof(szCause));
    TRACE("%s\n", szCause);
    return FALSE;
    }
    catch(...)
    {
    return FALSE;
    }
    }
      

  3.   

    在 "stdafx.cpp"中加入
    #import "c:\Program Files\Common Files\System\ADO\msado15.dll"  no_namespace rename("EOF", "EndOfFile")
    程序中的代码 :
    _ConnectionPtr m_pConnection;
       _RecordsetPtr m_pRecordset;
       _CommandPtr m_pCommand;
       m_pConnection.CreateInstance(__uuidof(Connection)); //初始化Connection指针
       m_pRecordset.CreateInstance(__uuidof(Recordset));//初始化Recordset指针
       m_pCommand.CreateInstance(__uuidof(Command));//初始化Command指针
       strDSN   =   "driver={SQL   Server};server="192.168.10.251";database="test";uid="sa";pwd="sa";";   
      

  4.   

    呵呵,有没有不是ODBC的
    上面的代码说_ConnectionPtr没有定义
      

  5.   

    请问有没有 用 ADO的详细步骤和代码
    谢谢
      

  6.   

    用别人封装的类吧,简单方便,代码都有。
    http://www.vckbase.com/document/viewdoc/?id=1557
    碰到问题再给我发站内信。