1 新建一个txt文件;
2 把后缀名改成.udl(txt->udl)
3 在右键,属性-连接-进行信息配置-测试连接;
4 在把.udl->txt;
5 恭喜您,成功了;
// 配置函数
bool CDatabase::configurationInfomation()
{
CoInitialize(NULL);
// 设置数据库连接信息
int hr;
try
{
// m_DatabaseScore.Format("Provider=SQLOLEDB.1;Password=%s;User ID=%s",m_userName,m_passWord);
//1 创建Connection对象
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
CString szSQL;
CString name = CString("sa");
CString pass = "sa";
CString Source=szVariable;//CString("XP-201112311130\\HIVIEW");
szSQL.Format("Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;\
Initial Catalog=master;Data Source=%s",pass,name,Source);
hr = m_pConnection->Open((_bstr_t)szSQL,"","",adModeUnknown);//PC\\SQLEXPRESS master
}
else
{
// this->MessageBox("初始化connection 失败");
cout<<"初始化 Connectiong 失败"<<endl;
}
//2 创建Recordptr对象
hr= m_pRecordSet.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr)) ;else cout<<"初始化 ADODB.Recordset 失败"<<endl;
//3 创建Command对象
hr =m_pCommand.CreateInstance("ADODB.Command");
if(SUCCEEDED(hr)) ;else cout<<"初始化 ADODB.Command 失败"<<endl;
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
cout<<"连接数据库失败!/r/n错误信息:%s"<<e.ErrorMessage()<<endl;
return FALSE;
}
//信息配置成功
return true;
}
// 设置数据库备份
bool CDatabase::backupDatabase()
{
try
{
m_pCommand->ActiveConnection = m_pConnection;
m_str.Format("backup database HiView to disk = %s 1with init",m_strPath);
m_pCommand->CommandText=_bstr_t(m_str);
m_pRecordSet = m_pCommand->Execute(NULL,NULL,adCmdText);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("备份数据库失败!/r/n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
// cout<<"备份数据库失败!/r/n错误信息:%s"<<e.ErrorMessage()<<endl;
return false;
}
// this->MessageBox("恭喜您,数据库备份成功!");
return true;
}