vc连接数据库设置配置信息

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值