专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

如何远程备份SQL_java

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/3/9 3:21:26       共计:3566 浏览

如何远程备份SQL?

方法一(不使用SQLDMO):

///

///备份方法

///

SqlConnection conn = new SqlConnection("Server=.;Database=master;User ID=sa;Password=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=master;User ID=sa;Password=sa;Trusted_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 = 0; i < list.Count; i++)

{

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=master;User ID=sa;Password=sa;Trusted_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 = 0; i < list.Count; i++)

{

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;

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:华为matepadPro怎么样_java | ·下一条:jc是什么意思C语言_java

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有