|
|
|
VC连接SQL SERVER数据库 |
|
|
作者:未知 来源:未知 加入时间:2005-9-11 人气:2229 |
您的位置: 首页 > Visual C++ > VC连接SQL SERVER数据库
VC连接SQL SERVER数据库
作者: werther161 时间: 2005-9-9 11:34:04 浏览: 0
来自:
关键词: VC连接SQL SERVER数据库
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();//释放对象
}
}

|
|
相关文章:
相关软件:
|
|
|