Home >Database >Mysql Tutorial >VC连接SQL SERVER数据库

VC连接SQL SERVER数据库

WBOY
WBOYOriginal
2016-06-07 15:11:271134browse

欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入 void smsLogon::InitSQLServer(CString server, CString db, CString UserName, CString Pwd) { m_pConnection.CreateInstance("ADODB.Connection"); CString strCn; strCn.Empty(); strCn="prov

欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入

    void smsLogon::InitSQLServer(CString server, CString db, CString UserName, CString Pwd)

    {

    m_pConnection.CreateInstance("ADODB.Connection");

    CString strCn;

    strCn.Empty();

    strCn="provider=SQLOLEDB;data source="+server

    +";initial catalog="+db

    +";userID="+UserName

    +";PassWord="+Pwd;

    _variant_t bcnstr=_variant_t(strCn);

    _variant_t bunstr=_variant_t(UserName);

    _variant_t bpwdstr=_variant_t(Pwd);

    //打开一个连接

    try

    {

    m_pConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),

    _bstr_t(bpwdstr),-1);//adOpenUnspecified

    }

    catch(_com_error e)

    {

    CString errormessage;

    errormessage.Format("Warning: 连接数据库发生异常. 错误信息: %s; 文件: %s; 行: %d\n", e.ErrorMessage(), __FILE__, __LINE__);

    AfxMessageBox(errormessage);

    }

    catch(...)

    {

    AfxMessageBox("Warning :连接数据库时发生未知错误");

    }

    }

    void smsLogon::ExitDB()     //退出时关闭数据库连接

    {

    if(m_pConnection!=NULL)

    {

    m_pConnection->Close();//关闭连接

    m_pConnection.Release();//释放对象

    }

    }

 

VC连接SQL SERVER数据库

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