sql远程备份
_ConnectionPtr m_pConnection;// 创建到Master数据库的连接 ,注意是master数据库
try
{
// 创建Connection对象
m_pConnection.CreateInstance( "ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strMasterConnect="Provider=SQLOLEDB; Server="+databaseip+";Database=master; uid=sa; pwd=sa;";
// _bstr_t strMasterConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source="+databaseip+"";
// _bstr_t strMasterConnect=str;
m_pConnection-> Open(strMasterConnect, "", "",adModeUnknown);
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
m_pConnection->Close();
m_pConnection=NULL;
AfxMessageBox(e.Description());
return 0;
}
TCHAR szPath3[MAX_PATH];
strcpy( szPath3, szPath2 );
strcat( szPath3, "\\*.bak" );
CFileDialog dlg(FALSE,"",szPath3,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"(*.bak)|*.bak|",NULL);
if(dlg.DoModal()!=IDOK)
return 0;
m_file=dlg.GetPathName();
str3=dlg.GetFileName();
int findindex=FindingString(m_file.GetBuffer(0),str3.GetBuffer(0),0);
str5.Empty();
for(i=0;i<findindex;i++)
{
str5+=m_file.GetAt(i);
}
str3=dlg.GetFileName();
str4.Empty();
for(i=0;i<str3.GetLength()-4;i++)
{
str4+=str3.GetAt(i);
}
// str3.Format("%s\\",szPath2);
str3=str5;
str3+=str4;
str6.Empty();
for(i=0;i<str5.GetLength()-1;i++)
str6+=str5.GetAt(i);
str8=str6;
findindex=FindingString(str6.GetBuffer(0),"\\",0);
if(findindex<0)
{
str6="bakeup";
AfxMessageBox("请选择备份文件夹!");
return 0;
}
while(findindex>=0)
{
str7.Empty();
for(i=findindex+1;i<str6.GetLength();i++)
str7+=str6.GetAt(i);
str6=str7;
findindex=FindingString(str6.GetBuffer(0),"\\",0);
}
CPasswordDlg PasswordDlg;
if(PasswordDlg.DoModal()!=IDOK)
{
m_pConnection->Close();
m_pConnection=NULL;
m_com.OnInitADOConn();
return 0;
}
CWiCNet* pNetFunctionCaller = new CWiCNet;
strcpy( szPath2, str8.GetBuffer(0) );
while(!pNetFunctionCaller->NetShareAdd( szPath2, str6.GetBuffer(0), "", 0, "") )
{
pNetFunctionCaller->NetShareDel(str6.GetBuffer(0));
// pNetFunctionCaller->NetShareAdd(szPath2, "bakeup", "", 0, "");
}
char UserName[255];
unsigned long a=255;
GetUserName(UserName, &a);
str.Format("%s",UserName);
str1.Empty();
CString strhost;
char szHostName[200];
//获得主机名
gethostname( szHostName, strlen( szHostName ) );
strhost.Format("%s",szHostName);
str1=PasswordDlg.m_str;
try
{
vSQL="xp_cmdshell 'net use \\\\"+localip+"\\"+str6+" "+str1+"/user:"+strhost+"\\"+str+"'";//str1为计算机登录密码,str为计算机登录用户名,strhost为计算机名
m_pConnection-> Execute(vSQL,NULL,adCmdText);
}
catch(_com_error e)
{
m_pConnection->Close();
m_pConnection=NULL;
pNetFunctionCaller->NetShareDel(str6.GetBuffer(0));
AfxMessageBox(e.Description());
return 0;
}
// szPath2
try
{
vSQL="backup database relacart to disk='\\\\"+localip+"\\"+str6+"\\"+dlg.GetFileName()+"' WITH init";
m_pConnection-> Execute(vSQL,NULL,adCmdText);
}
catch(_com_error e)
{
m_pConnection->Close();
// m_pConnection->Release();
m_pConnection=NULL;
pNetFunctionCaller->NetShareDel(str6.GetBuffer(0));
// vSQL="xp_cmdshell 'net use z: /delete'";
// m_pConnection-> Execute(vSQL,NULL,adCmdText);
AfxMessageBox(e.Description());
return 0;
}
if( !pNetFunctionCaller->NetShareDel(str6.GetBuffer(0)) )
{
AfxMessageBox("Failed!");
delete pNetFunctionCaller;
return 0;
}
delete pNetFunctionCaller;
麻烦帮我看看上面代码,现在我要实现的是局域网内sqlserver数据库的远程备份还原。
我现在用安装系统的的用户名Administrator,密码为空就可以实现,但修改用户名或者密码就不能实现,请高手请教。