C 運算元據庫備份和還原

2021-07-16 02:07:46 字數 2648 閱讀 1891

c#實現sqlserver2000資料庫備份還原的兩種方法

: 方法一(不使用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";

trycatch(exception ex)

finally

//////還原方法

///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())

dr.close();

for(int i = 0; i < list.count; i++)

", list), conn);

cmd.executenonquery();

}sqlcommand cmdrt = new sqlcommand();

cmdrt.commandtype = commandtype.text;

cmdrt.connection = conn;

cmdrt.commandtext = @"restore database test from disk='c:ba'";

trycatch(exception ex)

finally

方法二(使用sqldmo):

//////備份方法

/ backup = new sqldmo.backupclass();

sqldmo.sqlserver server = new sqldmo.sqlserverclass();

//顯示進度條

sqldmo.backupsink_percentcompleteeventhandler progress = new sqldmo.backupsink_percentcompleteeventhandler(step);

backup.percentcomplete += progress;

trycatch(exception ex)

finally

this.pbdb.value = 0;

//////還原方法

/ 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("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())

dr.close();

for(int i = 0; i < list.count; i++)

", list), conn);

cmd.executenonquery();

}conn.close();

trycatch(exception ex)

finally

this.pbdb.value = 0;

c 運算元據庫

namespace ec region sql資料庫備份 sql資料庫備份 sql伺服器ip或 localhost 資料庫登入名 資料庫登入密碼 資料庫名 備份到的路徑 public static void sqlback string serverip,string loginname,strin...

C 運算元據庫

先從資料庫中取出結果集後進行處理資料後再update更新到資料庫。如果只想讀取和顯示資料,只需要使用資料讀取sqldatareader即可,但要處理資料然後更新資料庫 增加 更改 局需要資料集dataset和資料介面卡sqldataadaper。sqldataadapter的用法 讀取資料用sqld...

C 運算元據庫

先上 using system using system.collections.generic using system.linq using system.text using system.threading.tasks using system.data using system.data....