VC运行之后出现这个,是什么问题啊?数据库都已经配置好了~~~
该怎么解决?
连接数据库的代码如下:
//-----数据库的还原
//-----连接master数据库
_ConnectionPtr pConnection;
try
{
HRESULT hr = pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
//hr = pConnection->Open("driver={SQL Server};Server=.;DATABASE=master;UID=sa;PWD=","","",adModeUnknown);//2000版本
hr = pConnection->Open("driver={SQL Server};Server=Z360-PC\XUEXIAOXU;DATABASE=master;","","",adModeUnknown);//2005及以上版本
//hr = pConnection->Open("driver={SQL Server};Server=HC\\MSSQL2008;DATABASE=master; UID=sa; PWD=", "","",adModeUnknown);
}
}
catch (_com_error e) //COM错误取得,当执行COM功能的时候,如果出错,可以捕捉到_com_error的异常
{
CString strComError;
strComError.Format("错误编号: %08lx\n错误信息: %s\n错误源: %s\n错误描述: %s",
e.Error(), // 错误编号
e.ErrorMessage(), // 错误信息
(LPCSTR) e.Source(), // 错误源
(LPCSTR) e.Description()); // 错误描述
::MessageBox(NULL,strComError,"错误",MB_ICONEXCLAMATION);
}
//------判断数据库是否存在
_RecordsetPtr pRecordset;
pRecordset.CreateInstance("ADODB.Recordset");
_variant_t vCount;
try
{
_variant_t ra;
pRecordset = pConnection->Execute("SELECT COUNT(*) FROM sysdatabases WHERE name = 'HMSys'",&ra,adCmdText);
vCount = pRecordset->GetCollect((_variant_t)(long)(0));
}
catch(_com_error e)
{
AfxMessageBox("error!");
}
CString str;
str.Format("%d", vCount.iVal);
if(0 == vCount.iVal)
{
CString Sql;
Sql.Format("USE MASTER RESTORE DATABASE HMSys FROM DISK = '%s' WITH MOVE 'HMSys_Data' TO 'c:\\HMSys.mdf', MOVE 'HMSys_Log' TO 'c:\\HMSys.ldf'", FindFilePath()) ;
try
{
_variant_t ra;
pRecordset = pConnection->Execute((_bstr_t)Sql,&ra,adCmdText);
pRecordset->Close();
}
catch(_com_error e)
{
}
}
pConnection->Close();
///////////////
//-----连接HMSys数据库
HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
try
{
if(SUCCEEDED(hr))
{