我现在要做个数据库程序,要求把服务器上的数据库复制到本地,也就相当于远程备份数据库。
听人说要先在服务器备份在考到本机上来
可在执行备份时出现如下错误
//代码
string ConnectionString = "server=ibm;uid=sa;pwd=sa;database=cs";
SqlConnection sqlConct = new SqlConnection(ConnectionString);
sqlConct.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConct;
sqlCommand.CommandText = "backup database cs to disk='D:\bs.bak' with init";
int a=sqlCommand.ExecuteNonQuery();
//错误
无法打开备份设备 'D:\bs.bak'。设备出现错误或设备脱机。详细信息请参阅 SQL Server 错误日志。
BACKUP DATABASE 操作异常终止。
就算备份成功那么怎么才能下载到本地呢?
听人说要先在服务器备份在考到本机上来
可在执行备份时出现如下错误
//代码
string ConnectionString = "server=ibm;uid=sa;pwd=sa;database=cs";
SqlConnection sqlConct = new SqlConnection(ConnectionString);
sqlConct.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConct;
sqlCommand.CommandText = "backup database cs to disk='D:\bs.bak' with init";
int a=sqlCommand.ExecuteNonQuery();
//错误
无法打开备份设备 'D:\bs.bak'。设备出现错误或设备脱机。详细信息请参阅 SQL Server 错误日志。
BACKUP DATABASE 操作异常终止。
就算备份成功那么怎么才能下载到本地呢?
exec sp_addumpdevice 'disk','test','E:\test.bak'backup DataBase cs to test--test是逻辑设备名称, test.bak就是备份文件了
压缩下载文件,实现输出
使用程序目录
ftpwebrequest下载
exec xp_cmdshell 'COPY D:\bs.bak C:\Inetpub\wwwroot\web1\bs.zip' 下载
cmd.Connection = sqlConct;
cmd.CommandText = @"exec xp_cmdshell 'COPY D:\bs.bak C:\Inetpub\wwwroot\downloads\bs.zip' ";
cmd.ExecuteNonQuery(); System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile("http://192.168.0.1/downloads/bs.zip", @"C:\bs.bak");
是什么意思
或者自己写个C/S 文件传输程序
你应该连接master库
C:\Inetpub\wwwroot\downloads\bs.zip
是把文件复制到一个可通过IIS访问的位置另外如果你不能控制这个服务器(没有管这个服务器的权利),还是算了吧,一般安全性高点的数据库服务器,管理员都是禁用'xp_cmdshell'的,因为xp_cmdshell配合SQL注入攻击,可以很容易的取得服务器的控制权
你必须在服务器上的wwwroot中建downloads这个目录,然后给这个目录添加Internet用户读取的权限
你可以打开SQL 查询分析器,在里面执行 exec xp_CmdShell '命令行,参数'
和你在CMD中执行的方法一样.
比如:
USE Master
exec xp_cmdShell 'ipconfig/all>>c:\r.txt'
exec xp_CmdShell 'type c:\r.txt'
GO具体的命令,你去查相关文档吧,太多,列举不清.......