result= this.ExecutesAdmin("backup database tb_Sell to disk = '" + this.Server.MapPath("./Bak/" + GetDateTime()) + "' with noinit"); private bool ExecutesAdmin(string sqlstring)
{
try
{
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
SqlCommand scd = new SqlCommand(sqlstring,strcon);
scd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
Response.Write("<script>alert('操作异常');location='AddDBBak.aspx'</script>");
return false;
}
}
这是我写的关于数据备份的代码,但是编译后总是跳出“操作异常”页面,哪位高手给指点一下。
{string Path =FileName;
//实例化SQLDMO的各个对象
SQLDMO.Backup backup = new SQLDMO.BackupClass();
SQLDMO.SQLServer server = new SQLDMO.SQLServerClass(); try
{
//设置安全验证
server.LoginSecure=false;
//设置连接数据库
server.Connect("(local)",uid,pwd);
backup.Action=0; //方法绑定
SQLDMO.BackupSink_PercentCompleteEventHandler percent = new SQLDMO.BackupSink_PercentCompleteEventHandler(ShowProcessBar);
backup.PercentComplete +=percent;
//设置备份的数据库
backup.Database="LibraryClass";
//设置备份文件名
backup.BackupSetName="LibraryClassbackup";
//设置备份文件描述
backup.BackupSetDescription="BackUp";
Path = Path + backup.BackupSetName.ToString() ;
//设置存储路径
backup.Files=@Path;
backup.Initialize=true;
//设置备份服务器--与server关联
backup.SQLBackup(server); MessageBox.Show("备份成功!");
catch(Exception ex)
{
MessageBox.Show("备份失败,请检查!" +"\n" + ex.Message.ToString() );
}
finally
{
//释放对象
server.DisConnect();
server=null;
backup=null;
}
}
private void ShowProcessBar(string message,int percent)
{
//显示进度条
this.pbProcess.Value=percent;
}
strcon.Open();
SqlCommand scd = new SqlCommand(sqlstring,strcon);
执行下一句时就跳出来了哪个高人能帮我分析一下?
result= this.ExecutesAdmin("backup database tb_Sell to disk = '" + this.Server.MapPath("./Bak/" + GetDateTime()) + "' with noinit");
你把+ this.Server.MapPath("./Bak/" + GetDateTime()) + 这个改成一绝对路径比如"c:\\aa.bak"再去测试看看,
我这边这样测试是没有错的