WEB开发网
开发学院软件开发C++ VC连接SQL SERVER数据库 阅读

VC连接SQL SERVER数据库

 2008-03-08 21:59:14 来源:WEB开发网   
核心提示:void smsLogon::InitSQLServer(CString server, CString db, CString UserName, CString Pwd){m_pConnection.CreateInstance("ADODB.Connection");CString strCn
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();//释放对象

}

}

Tags:VC 连接 SQL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接