欢迎进入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();//释放对象
}
}