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

MFC连接Sqlserver - osbreak

PHPz
Release: 2018-09-28 14:16:27
Original
1788 people have browsed it

 

下载 ado2.h和ado2.cpp文件
Copy after login

 

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

 

// 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
Copy after login

 

连接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;
}
Copy after login

 

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();
}
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template