asp教程.net c# 資料庫教程備份與還原二種方法(sql server )
下面主要是講一下關於利用c#來備份mssql server資料庫了。下面看代碼,
///備份方法
///
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( "select spid from sysprocesses ,sysdatabases where sysprocesses.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), conn);
cmd.executenonquery();
}
sqlcommand cmdrt = new sqlcommand();
cmdrt.commandtype = commandtype.text;
cmdrt.connection = conn;
cmdrt.commandtext = @ "restore database test from disk= 'c:ba ' ";
try
{
cmdrt.executenonquery();
messagebox.show( "restore successed. ");
}
catch(exception ex)
{
messagebox.show(ex.message);
}
finally
{
conn.close();
}
方法二
( 用sql語句實現對Database Backup還原作業
備份sqlserver資料庫:
backup database 資料庫名 to disk (備份檔案存放路徑+檔案名稱).bak
還原sqlserver資料庫:
string path = this.fileupload1.postedfile.filename; //獲得備份路徑及資料庫名稱
use master restore database 資料庫名 from disk='" + path + "'"; )
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.collections;
using system.data.sql;
using system.io;
namespace Database Backup
{
public partial class form1 : form
{
//需要添加一個引用c:program filesmicrosoft sql server80toolsbinn下的sqldmo.dll;
//sql預設安裝下是在以上路徑
//備份的檔案放在c:program filesmicrosoft sql servermssqlbackup下
string servername = "";
string username = "sa";//暫時鎖定,可以根據需要自己設定
string password = "sa";
public form1()
{
initializecomponent();
}
//擷取伺服器列表
public arraylist getserverlist()
{
arraylist alservers = new arraylist();
sqldmo.application sqlapp = new sqldmo.applicationclass();
try
{
sqldmo.namelist serverlist = sqlapp.listavailablesqlservers();
for (int i = 1; i <= serverlist.count; i++)
{
alservers.add(serverlist.item(i));
//combobox1.items.add(serverlist.item(i));
listbox1.items.add(serverlist.item(i));
}
}
catch (exception e)
{
throw (new exception("取資料庫伺服器列表出錯:" + e.message));
}
finally
{
sqlapp.quit();
}
return alservers;
}
//擷取資料庫列表
public arraylist getdblist(string strservername, string strusername, string strpwd)
{
string servername = strservername;
string username = strusername;
string password = strpwd;
arraylist aldbs = new arraylist();
sqldmo.application sqlapp = new sqldmo.applicationclass();
sqldmo.sqlserver svr = new sqldmo.sqlserverclass();
try
{
svr.connect(servername, username, password);
foreach (sqldmo.database db in svr.databases)
{
if (db.name != null)
aldbs.add(db.name);
listbox2.items.add(db.name);
}
}
catch (exception e)
{
messagebox.show("串連資料庫出錯:" + e.message);
}
finally
{
svr.disconnect();
sqlapp.quit();
}
return aldbs;
}
//備份資料
public bool backupdb(string strdbname, string strfilename, progressbar pgbmain)
{
progressbar pbar = pgbmain;
sqldmo.sqlserver svr = new sqldmo.sqlserverclass();
try
{
svr.connect(servername, username, password);
sqldmo.backup bak = new sqldmo.backupclass();
bak.action = 0;
bak.initialize = true;
sqldmo.backups教程ink_percentcompleteeventhandler pceh = new sqldmo.backupsink_percentcompleteeventhandler(step);
bak.percentcomplete += pceh;
bak.files = strfilename;//這裡可以寫成路徑+檔案名稱形式,自己寫!
bak.database = strdbname;
bak.sqlbackup(svr);
return true;
}
catch (exception err)
{
throw (new exception("備份資料庫失敗" + err.message));
}
finally
{
svr.disconnect();
}
}
//進度條
private void step(string message, int percent)
{
pbar.visible = true;
pbar.value = percent;
}