
2、链接上远程服务器,选中要备份的数据库--》右击--》任务--》生成脚本
然后根据向导执行,但是在“选择脚本选项”页面要把
编写数据的脚本
的选项
设置成TRUE,
然后下一步,选择你要备份的表、存储过程、视图等
知道最后生成一个.sql文件,在本地执行就好了。
一:备份远程数据库,将备份文件拷贝到本地,然后在本地还原数据库。二:将远程数据库的数据文件跟日志文件拷贝到本地(拷贝之前需要先停止远程数据库服务,否则无法进行拷贝),然后在本地进行附加数据库 *** 作。
三:(需要知道远程数据库的登录名跟密码)打开企业管理器,在“sql server”组上右键选择“新建sql server注册”,点下一步,在“可用的服务器”中输入远程服务器的IP,然后点击“添加”,继续下一步,一步一步按照指导完成。注册完成后就可以像 *** 作本地数据库一样对远程数据库进行 *** 作了backup database sys to disk='\\你的ip\共享目录\a.bak' with init有一点是很关键的,就是启动你远程sql server数据库服务的帐号需要有在本地驱动器共享文件夹写的权限。要不,也是无法进行备份的。
手头上只有C#的代码,不知道对你有没有帮助远程备份要注意权限的问题,普通的访问帐户没有备份和还原的权限,最好用sa的帐户,并且程序访问的帐户必须添加到远程机器的系统内
你说的远程备份到本地是不可能的,你只有开放远程某个文件夹加入你的特殊访问帐户权限
最好直接用远程桌面,这样最方便
方法一(不使用SQLDMO):
///
///备份方法
///
SqlConnection conn = new SqlConnection("Server=.Database=masterUser ID=saPassword=sa")
SqlCommand cmdBK = new SqlCommand()
cmdBK.CommandType = CommandType.Text
cmdBK.Connection = conn
cmdBK.CommandText = @"backup database test to disk='C:\ba' with init"
try
{
conn.Open()
cmdBK.ExecuteNonQuery()
MessageBox.Show("Backup successed.")
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}
finally
{
conn.Close()
conn.Dispose()
}
///
///还原方法
///
SqlConnection conn = new SqlConnection("Server=.Database=masterUser ID=saPassword=saTrusted_Connection=False")
conn.Open()
//KILL DataBase Process
SqlCommand cmd = new SqlCommand("SELECTspidFROMsysprocesses ,sysdatabases WHEREsysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='test'", conn)
SqlDataReader dr
dr = cmd.ExecuteReader()
ArrayList list = new ArrayList()
while(dr.Read())
{
list.Add(dr.GetInt16(0))
}
dr.Close()
for(int i = 0i <list.Counti++)
{
cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn)
cmd.ExecuteNonQuery()
}
SqlCommand cmdRT = new SqlCommand()
cmdRT.CommandType = CommandType.Text
cmdRT.Connection = conn
cmdRT.CommandText = @"restore database test fromdisk='C:\ba'"
try
{
cmdRT.ExecuteNonQuery()
MessageBox.Show("Restore successed.")
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}
finally
{
conn.Close()
}
方法二(使用SQLDMO):
///
///备份方法
///
SQLDMO.Backup backup = new SQLDMO.BackupClass()
SQLDMO.SQLServer server = new SQLDMO.SQLServerClass()
//显示进度条
SQLDMO.BackupSink_PercentCompleteEventHandler progress = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step)
backup.PercentComplete += progress
try
{
server.LoginSecure = false
server.Connect(".", "sa", "sa")
backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
backup.Database = "test"
backup.Files = @"D:\test\myProg\backupTest"
backup.BackupSetName = "test"
backup.BackupSetDescription = "Backup the database of test"
backup.Initialize = true
backup.SQLBackup(server)
MessageBox.Show("Backup successed.")
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}
finally
{
server.DisConnect()
}
this.pbDB.Value = 0
///
///还原方法
///
SQLDMO.Restore restore = new SQLDMO.RestoreClass()
SQLDMO.SQLServer server = new SQLDMO.SQLServerClass()
//显示进度条
SQLDMO.RestoreSink_PercentCompleteEventHandler progress = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step)
restore.PercentComplete += progress
//KILL DataBase Process
SqlConnection conn = new SqlConnection("Server=.Database=masterUser ID=saPassword=saTrusted_Connection=False")
conn.Open()
SqlCommand cmd = new SqlCommand("SELECTspid FROMsysprocesses ,sysdatabases WHEREsysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='test'", conn)
SqlDataReader dr
dr = cmd.ExecuteReader()
ArrayList list = new ArrayList()
while(dr.Read())
{
list.Add(dr.GetInt16(0))
}
dr.Close()
for(int i = 0i <list.Counti++)
{
cmd = new SqlCommand(string.Format("KILL {0}", list[i]), conn)
cmd.ExecuteNonQuery()
}
conn.Close()
try
{
server.LoginSecure = false
server.Connect(".", "sa", "sa")
restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
restore.Database = "test"
restore.Files = @"D:\test\myProg\backupTest"
restore.FileNumber = 1
restore.ReplaceDatabase = true
restore.SQLRestore(server)
MessageBox.Show("Restore successed.")
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}
finally
{
server.DisConnect()
}
this.pbDB.Value = 0
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)