请教vc ado 数据库还原的问题

waittomorrow 2007-11-01 10:03:26
请教vc ado 数据库还原的问题
我下面的代码不知哪里错了,提示对象关闭时不允许操作,帮忙看看,谢谢
其中BillExport是要被还原的数据库,sPath是备份文件的路径
try
{
// 初始化OLE/COM库环境
::CoInitialize(NULL);
//添加一个指向Connection对象的指针:
_ConnectionPtr m_pConnection1;
//添加一个指向Recordset对象的指针:
_RecordsetPtr m_pRecordset1;
// 创建Connection对象
m_pConnection1.CreateInstance("ADODB.Connection");
// 创建记录集对象
m_pRecordset1.CreateInstance(__uuidof(Recordset));
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;PersistSecurity Info=False;Initial Catalog=master";
m_pConnection1->Open(strConnect,"","",adModeUnknown);
//关闭与BillExport相关的进程
CString temp,sql,sql1;
_bstr_t strDataRestore,str1;
sql = "select spid from master..sysprocesses where dbid=db_id('BillExport')";//查询与BillExport数据库连接的进程ID
strDataRestore = (_bstr_t)sql;
m_pRecordset1 = m_pConnection1->Execute(strDataRestore,NULL,adCmdText);

while (!m_pRecordset1->adoEOF)//断开与BillExport数据库的连接
{
temp = (TCHAR *)(_bstr_t)m_pRecordset1->GetFields()->GetItem("spid")->Value;
sql1.Format(" use master kill %i",atoi(temp));
str1 = (_bstr_t)sql1;
m_pConnection1->Execute((_bstr_t)str1,NULL,adCmdText);
m_pRecordset1->MoveNext();
}
CString sSQL;
_bstr_t vSQL;
sSQL.Format("USE master RESTORE DATABASE BillExport FROM DISK='%s'",sPath);
vSQL = (_bstr_t)sSQL;
m_pRecordset1 = m_pConnection1->Execute(vSQL,NULL,adCmdText);
// 关闭记录集和连接
if (m_pRecordset1 != NULL)
m_pRecordset1->Close();
m_pConnection1->Close();
// 释放环境
::CoUninitialize();
}
// 捕捉异常
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
编译通过,运行时提示对象关闭时不允许操作
...全文
219 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
一条晚起的虫 2007-11-13
  • 打赏
  • 举报
回复
MS SQL Server?
连接到master数据库,执行restore操作
shakaqrj 2007-11-13
  • 打赏
  • 举报
回复
while (!m_pRecordset1- >adoEOF)//断开与BillExport数据库的连接
{
temp = (TCHAR *)(_bstr_t)m_pRecordset1- >GetFields()- >GetItem( "spid ")- >Value;
sql1.Format( " use master kill %i ",atoi(temp));
str1 = (_bstr_t)sql1;
m_pConnection1- >Execute((_bstr_t)str1,NULL,adCmdText);
m_pRecordset1- >MoveNext();
}
会不会把自己建立的连接也kill掉了?
tsming 2007-11-13
  • 打赏
  • 举报
回复
学习
waittomorrow 2007-11-13
  • 打赏
  • 举报
回复
大概应该是这样的,结帖了,谢谢大家
waittomorrow 2007-11-13
  • 打赏
  • 举报
回复
不好意思,整错了,应该用下面的检测
if(m_pRecordset1-> GetState()==adStateOpen)
{
MessageBox( "没有关闭 ");
}
else
{
MessageBox( "关闭 ");
}
输出关闭。
不明白我还没有运行m_pRecordset1->Close();
怎么就已经关闭了呢?
是不是因为,返回的是BillExport的记录集,而BillExport已经关闭。所以返回的记录集也是关闭的。
另外在
m_pRecordset1=m_pConnection1->Execute(strDataRestore,NULL,adCmdText);
m_pRecordset1m_pConnection1->Execute(vSQL,NULL,adCmdText);
之间没有m_pRecordset1->Close();程序运行也正常。
waittomorrow 2007-11-13
  • 打赏
  • 举报
回复
谢谢
// 关闭记录集和连接
if(m_pRecordset1!=NULL)
m_pRecordset1-> Close(); //运行到这句时出现错误
m_pConnection1-> Close();
// 释放环境
::CoUninitialize();
}
上面m_pRecordset1-> Close(); //运行到这句时出现错误
提示对象关闭时不允许操作
我用下面的语句检测,正常输出“没有关闭”
if(m_pConnection1->GetState()==adStateOpen)
{
MessageBox("没有关闭");
}
既然没有关闭,为什么不能m_pRecordset1-> Close(); 呢
恢复的功能已实现,只差这点了
shakaqrj 2007-11-13
  • 打赏
  • 举报
回复
个人觉得要关闭记录集,就是m_pRecordset1-> Close();
waittomorrow 2007-11-13
  • 打赏
  • 举报
回复
shakaqrj
我试了下,发现恢复之前m_pConnection1没有关闭

上面的两次m_pRecordset1中间要关闭一次吗
是用 m_pRecordset1->Close();吗?
然后还要再次写下面的两条吗
_ConnectionPtr m_pConnection1;
m_pRecordset1.CreateInstance(__uuidof(Recordset));
shakaqrj 2007-11-13
  • 打赏
  • 举报
回复
1。你在恢复之前检查一下connection是不是已经关闭了
2。 突然发现你的m_pRecordset1好像在恢复之前没有关闭
m_pRecordset1 = m_pConnection1-> Execute(strDataRestore,NULL,adCmdText);
m_pRecordset1 = m_pConnection1-> Execute(vSQL,NULL,adCmdText);
waittomorrow 2007-11-13
  • 打赏
  • 举报
回复
谢谢你们
shakaqrj我的代码中有下面这句
sql="select spid from master..sysprocesses where dbid=db_id('BillExport')";
得到的spid。所以应该不会杀掉自己吧。
lfchen我用的数据库是sql server 2000.连接到master数据库,执行restore操作.我的代码中有体现的
可是不知道哪里错了。
或者你们是否有没有好的代码,是否可以分享一下,谢谢
waittomorrow 2007-11-05
  • 打赏
  • 举报
回复
分给的少了些,可是这是我最后的20分了

谁会呀
waittomorrow 2007-11-01
  • 打赏
  • 举报
回复
运行时数据库没有打开,企业管理器也没有打开
即使打开了,下面的while也能关掉吧
while (!m_pRecordset1- >adoEOF)//断开与BillExport数据库的连接
shakaqrj 2007-11-01
  • 打赏
  • 举报
回复
运行时数据库打开的?
waittomorrow 2007-11-01
  • 打赏
  • 举报
回复
谢谢
sSQL.Format("USE master RESTORE DATABASE BillExport FROM DISK='%s'",sPath);
vSQL = (_bstr_t)sSQL;
m_pRecordset1 = m_pConnection1->Execute(vSQL,NULL,adCmdText);
调试执行到上面这条语句时出现了错误,提示 对象关闭时不允许操作
去掉try和catch后,执行到上面这条语句后提示
Runtime Error
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
不清楚怎么解决错误
cpio 2007-11-01
  • 打赏
  • 举报
回复

肯定是自己被断掉了才出现对象关闭时不允许操作

不要Try,调试,看哪儿出的问题

4,011

社区成员

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

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