怎么用vc调用SQL Server写的存储过程
1个回答
展开全部
HRESULT hr; ///设定一个句柄
_ConnectionPtr pConnection; ///一个连接指针
CString strSQLConnect = ((CManagerSys1App*)AfxGetApp())->m_strConnectMaster;
try
{
hr = pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = pConnection->Open((_bstr_t)strSQLConnect,"","",adModeUnknown); ///连接数据
_variant_t RecordsAffected;
CString strDBName = ((CManagerSys1App*)AfxGetApp())->m_strDBName;
CString strKill;
strKill.Format("exec p_killspid '%s'",strDBName); //kill all connected link
pConnection->Execute((_bstr_t)strKill,&RecordsAffected,adCmdText);
CString strBackup;
strBackup.Format("restore database %s from disk='",strDBName); //restore
strBackup += "C:\\temp.bak";
strBackup += "' with replace";
pConnection->Execute((_bstr_t)strBackup,&RecordsAffected,adCmdText);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("全系统数据恢复失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
///关闭数据库连接
if((pConnection != NULL))
{
if(pConnection->State)
pConnection->Close();
pConnection = NULL;
}
return;
}
_ConnectionPtr pConnection; ///一个连接指针
CString strSQLConnect = ((CManagerSys1App*)AfxGetApp())->m_strConnectMaster;
try
{
hr = pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = pConnection->Open((_bstr_t)strSQLConnect,"","",adModeUnknown); ///连接数据
_variant_t RecordsAffected;
CString strDBName = ((CManagerSys1App*)AfxGetApp())->m_strDBName;
CString strKill;
strKill.Format("exec p_killspid '%s'",strDBName); //kill all connected link
pConnection->Execute((_bstr_t)strKill,&RecordsAffected,adCmdText);
CString strBackup;
strBackup.Format("restore database %s from disk='",strDBName); //restore
strBackup += "C:\\temp.bak";
strBackup += "' with replace";
pConnection->Execute((_bstr_t)strBackup,&RecordsAffected,adCmdText);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("全系统数据恢复失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
///关闭数据库连接
if((pConnection != NULL))
{
if(pConnection->State)
pConnection->Close();
pConnection = NULL;
}
return;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询