数据库还原的详细代码,帮助那些需要的朋友!

quiton 2007-06-21 02:15:00
昨天发帖子,一直没有得到非常明确的答案,今天终于写出来了,发给需要的朋友,希望对大家有用用!


try
{
GetDlgItem(IDC_PROCESSINGRESTORE)->SetWindowText("正在还原,请稍后...");

theApp.m_pConnection->Close();//关闭与数据库连接
theApp.m_pConnection->Open("Provider=SQLOLEDB;Server=LENOVO-D01793DE;DATABASE=master;UID=sa;PWD=;","","",adModeUnknown);//连接master数据库

sql = "select spid from master..sysprocesses where dbid=db_id('Signal')";//查询与Signal数据库连接的进程ID
strDataRestore = (_bstr_t)sql;
theApp.ADOExecute(theApp.m_pRecordset, strDataRestore);

while (!theApp.m_pRecordset->adoEOF)//利用循环将各进程断开与Signal数据库的连接
{
temp = (TCHAR *)(_bstr_t)theApp.m_pRecordset->GetFields()->GetItem("spid")->Value;
sql1.Format(" use master kill %i",atoi(temp));
str1 = (_bstr_t)sql1;
theApp.m_pConnection->Execute((_bstr_t)str1,NULL,adCmdText);
theApp.m_pRecordset->MoveNext();
}

strSQL = "use master RESTORE DATABASE Signal FROM DISK = 'c:\\a.db'";
strDataRestore = (_bstr_t)strSQL;
theApp.m_pConnection->Execute((_bstr_t)strDataRestore,NULL,adCmdText);//通过SQL语句进行数据库备份
GetDlgItem(IDC_PROCESSINGRESTORE)->SetWindowText("");

theApp.m_pConnection->Close();//关闭与数据库连接
theApp.m_pConnection->Open("Provider=SQLOLEDB;Server=LENOVO-D01793DE;DATABASE=Signal;UID=sa;PWD=;","","",adModeUnknown);//连接Signal数据库

MessageBox("还原成功.","提示",64);
}
catch (...)
{
MessageBox("还原失败.","提示",64);
}
...全文
152 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧