Home >Database >Mysql Tutorial >MFC连接Sqlserver - osbreak

MFC连接Sqlserver - osbreak

PHPz
PHPzOriginal
2016-06-07 15:51:231803browse

 

下载 ado2.h和ado2.cpp文件

 

在VC++ 目录-->包含目录 -->添加  msado15.dll, msjro.dll 目录。

 

// TODO: 连接sqlserver, 在stdafx.h 中定义这些,#define _BS_DLL_EXPORT_# ifdef _BS_DLL_EXPORT_
# define BS_DLL_EXPORT __declspec(dllexport)
# else# define BS_DLL_EXPORT __declspec(dllimport)
# endif

 

连接sqlserver数据库bool connectToSqlServer()
{
    CADODatabase *g_pAdoDatabase;    try{        if (g_pAdoDatabase == NULL)
                g_pAdoDatabase = new CADODatabase();

        CString strConnString = "Provider=SQLOLEDB;Persist Security Info=False;Data Source=" + \
            strServer + ";Initial Catalog=" + strDatabase + ";User Id=" + strUser + ";Password=" + strPwd;

        g_pAdoDatabase->SetConnectionString((LPCTSTR)strConnString);
    }    catch (...)
    {        return false;
    }    return true;
}

 

if(g_pAdoDatabase->Open())
{        // 查询
        CString sqlText = "select ...";
         
        CADORecordset* pRs = new CADORecordset(g_pAdoDatabase);        if(pRs->Open((LPCTSTR)sqlText))
        {            while (!pRs->IsEof())
            {
                pRs->GetFieldValue("id", ID);
                pRs->MoveNext();
            }
        }
        pRs->Close();
        delete pRs;        
}catch (...)
{
    return false;
}if(g_pAdoDatabase->IsOpen())
{
    g_pAdoDatabase->Close();
}

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn