高手请进,一个关于几种数据库访问方式同时操纵SQL数据库的问题

jamiandy 2006-03-24 09:15:09
我的程序程序中由于设计时用的是DAO,那时我觉得DAO用起来很简单.
昨天要实现一个数据备份和还原的功能,备份到还好办,可是还原DAO就显得力不从心了。
昨晚上网看了很多在程序中实现数据库还原的方法,都是通过ADO来操作SQL数据库的。
可是,我的程序是用DAO写的,现在已经快完成了,也不可能做整体修改了,所以,我就想可不可以吧两种方式混合在以起对数据库操作。
然后,我就写了一些代码,在经过几个小时的修改调试后,还原数据库到是实现了,就是以前我打开的表,怎么也打不开了,我想可能是由于ADO的某些原因引起的,别的原因差不多都可以排除,因为,我已经在程序中写过类似的代码,都工作得很好,除了没有用到ADO之外。
下边是我的程序:
CString strSQL;
//获取文件路径的对话框
CBackAndRestoreDlg dlg;
dlg.m_bIsBackup=FALSE;
if(dlg.DoModal()==IDOK)
{

strSQL="RESTORE DATABASE db FROM DISK=";
strSQL+="'";
dlg.m_strPath.TrimLeft();
dlg.m_strPath.TrimRight();
strSQL+=dlg.m_strPath;
strSQL+="' WITH REPLACE";

//MessageBox(strSQL);

//将记录集和数据库关闭
g_nCurrent=g_pSet->GetAbsolutePosition();
if(g_pSet->IsOpen())
g_pSet->Close();
if(((CDataApp*)AfxGetApp())->m_database.IsOpen())
((CDataApp*)AfxGetApp())->m_database.Close();

//引用ADO的命名空间
using namespace ADONS;
//连接到新的数据库对其进行操作
CString strSRC;
strSRC="Driver=SQL Server;Server=";
strSRC+="127.0.0.1";
strSRC+=";Database=";
strSRC+="master";
strSRC+=";UID=;PWD=";

_variant_t varSRC(strSRC);
_bstr_t bstrSQL(strSQL);
_bstr_t bstrSRC(strSRC);

_ConnectionPtr pConn;

BeginWaitCursor();
HRESULT hr;
try
{
hr = pConn.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr =pConn->Open(bstrSRC,"","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
EndWaitCursor();
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
pConn.Release();
return;
}


_CommandPtr pCommand;
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection=pConn;
pCommand->CommandText="ALTER DATABASE db SET OFFLINE WITH ROLLBACK IMMEDIATE";
pCommand->Execute(NULL,NULL,adCmdUnknown);
pCommand->CommandText=bstrSQL;
try{
pCommand->Execute(NULL,NULL,adCmdUnknown);
EndWaitCursor();
MessageBox("数据库恢复成功","提示",MB_ICONWARNING);
}
catch(_com_error e)///捕捉异常
{
EndWaitCursor();
CString errormessage;
errormessage.Format("数据库恢复失败\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
pConn.Release();
return;
}

BeginWaitCursor();
pCommand->CommandText="ALTER DATABASE db SET ONLINE WITH ROLLBACK IMMEDIATE";
pCommand->Execute(NULL,NULL,adCmdUnknown);

pCommand->ActiveConnection=NULL;
pConn->Close();
pConn=NULL;

using namespace std;
//操作完成后重新打开数据库
CString strConnect="ODBC;";
try {
((CDataApp*)AfxGetApp())->m_database.Open("data", FALSE, FALSE,strConnect);
}
catch (CDaoException* e) {
//::DaoErrorMsg(e);
EndWaitCursor();
AfxMessageBox("不能连接到数据库,数据库可能不存在或服务器没有开启!");
e->Delete();
return;
}
g_strConnect = ((CDataApp*)AfxGetApp())->m_database.GetConnect();
TRACE("database name = %s, connect = %s\n",
(const char*) g_strDatabase,
(const char*) g_strConnect);
//打开表连接并恢复到以前
if(g_pSet->IsOpen())
g_pSet->Close();
change.Format("SELECT * FROM [%s]",g_strTable);
try {
g_pSet->Open(dbOpenDynaset, change);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
g_bConnected = FALSE;
e->Delete();
return;
}
g_pSet->MoveLast();
g_pSet->SetAbsolutePosition(g_nCurrent);
EndWaitCursor();
}
在测试的过程中,一直没有出现什么问题,到运行到打开g_strTable时,就报错,调试过程中,发生错误时的输出信息如下:
DAO Call Failed.
m_pQueryDef->m_pDAOQueryDef->_30_OpenRecordset( COleVariant((long)m_nOpenType), COleVariant((long)m_nOptions), &m_pDAORecordset)
In file daocore.cpp on line 3337
scode = 800A0C4A

Error Code = 4
Source = ODBC.Database
Description = [Microsoft][ODBC SQL Server Driver][Shared Memory]ConnectionWrite (WrapperWrite()).

Error Code = 11
Source = ODBC.Database
Description = [Microsoft][ODBC SQL Server Driver][Shared Memory]一般性网络错误。请检查网络文档。

Error Code = 3146
Source = DAO.Database
Description = ODBC--调用失败。
data.exe 中的 0x7c81eb33 处最可能的异常: Microsoft C++ exception: CDaoException @ 0x0013ee00 。
data.exe 中的 0x7c81eb33 处最可能的异常: Microsoft C++ exception: [rethrow] @ 0x00000000 。
各位大哥大姐,可得帮帮在下了,帮我想想有没有什么解决的办法,谢谢了。分不够可以再加。
...全文
138 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jamiandy 2006-03-25
  • 打赏
  • 举报
回复
我的贴都发了一天了,怎么还是没有人来回啊?
我都郁闷死了。
jamiandy 2006-03-25
  • 打赏
  • 举报
回复
我等得花儿也谢了,怎么还没有人来啊?
jamiandy 2006-03-24
  • 打赏
  • 举报
回复
怎么没有人啦?
高手都上哪儿去了?
怎么就没有人来帮帮我呢?

4,011

社区成员

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

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