使用C#对sqlserver数据库【备份】【还原】【分离】【附加】软件名称:SQLServer数据库小助手开发工具:MicrosoftVisualStudio2008开发语言:C#版本:2010.12.28.1软件预览2.设置数据库连接软件下载地址1:地址2:主要源码usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Windows.Forms;usingSystem.Configuration;usingemployment;namespace数据库小助手{publicclassDataBaseControl{///summary///数据库连接字符串////summary//stringConnectionString=string.Format(DataSource=({0});Userid={1};Password={2};InitialCatalog=master,UserHelper.sql.servername,UserHelper.sql.uid,UserHelper.sql.password);//stringConnectionString=DataSource=(local);Userid=sa;Password=123;InitialCatalog=master;stringConnectionString=string.Format(DataSource={0};InitialCatalog=master;PersistSecurityInfo=True;UserID={1};Password={2},UserHelper.sql.servername,UserHelper.sql.uid,UserHelper.sql.password);///summary///SQL操作语句/存储过程////summarypublicstringStrSQL;///summary///实例化一个数据库连接对象////summaryprivateSqlConnectionConn;///summary///实例化一个新的数据库操作对象Comm////summaryprivateSqlCommandComm;///summary///要操作的数据库名称////summarypublicstringDataBaseName;///summary///数据库文件完整地址////summarypublicstringDataBase_MDF;///summary///数据库日志文件完整地址////summarypublicstringDataBase_LDF;///summary///备份文件名////summarypublicstringDataBaseOfBackupName;///summary///备份文件路径////summarypublicstringDataBaseOfBackupPath;///summary///执行创建/修改数据库和表的操作////summarypublicvoidDataBaseAndTableControl(){try{Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand();Comm.Connection=Conn;Comm.CommandText=StrSQL;Comm.CommandType=CommandType.Text;Comm.ExecuteNonQuery();MyMessageBox.Show(数据库操作成功!,信息提示);}catch(Exceptionex){MyMessageBox.Show(ex.Message,信息提示);}finally{Conn.Close();}}///summary///附加数据库////summarypublicvoidAddDataBase(){try{Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand();Comm.Connection=Conn;Comm.CommandText=@sp_attach_db;Comm.Parameters.Add(newSqlParameter(@dbname,SqlDbType.NVarChar));Comm.Parameters[@dbname].Value=DataBaseName;Comm.Parameters.Add(newSqlParameter(@filename1,SqlDbType.NVarChar));Comm.Parameters[@filename1].Value=DataBase_MDF;Comm.Parameters.Add(newSqlParameter(@filename2,SqlDbType.NVarChar));Comm.Parameters[@filename2].Value=DataBase_LDF;Comm.CommandType=CommandType.StoredProcedure;Comm.ExecuteNonQuery();MyMessageBox.Show(附加数据库成功,信息提示);}catch(Exceptionex){MyMessageBox.Show(ex.Message,信息提示);}finally{Conn.Close();}}///summary///分离数据库////summarypublicvoidDeleteDataBase(){try{Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand();Comm.Connection=Conn;Comm.CommandText=@sp_detach_db;Comm.Parameters.Add(newSqlParameter(@dbname,SqlDbType.NVarChar));Comm.Parameters[@dbname].Value=DataBaseName;Comm.CommandType=CommandType.StoredProcedure;Comm.ExecuteNonQuery();MyMessageBox.Show(分离数据库成功,信息提示);}catch(Exceptionex){MyMessageBox.Show(ex.Message,信息提示);}finally{Conn.Close();}}///summary///备份数据库////summarypublicvoidBackupDataBase(){try{Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand();Comm.Connection=Conn;Comm.CommandText=usemaster;backupdatabase@dbnametodisk=@backupname;;Comm.Parameters.Add(newSqlParameter(@dbname,SqlDbType.NVarChar));Comm.Parameters[@dbname].Value=DataBaseName;Comm.Parameters.Add(newSqlParameter(@backupname,SqlDbType.NVarChar));Comm.Parameters[@backupname].Value=@DataBaseOfBackupPath+@DataBaseOfBackupName;Comm.CommandType=CommandType.Text;Comm.ExecuteNonQuery();MyMessageBox.Show(备份数据库成功,信息提示);}catch(Exceptionex){MyMessageBox.Show(ex.Message,信息提示);}finally{Conn.Close();}}///summary///还原数据库////summarypublicvoidReplaceDataBase(){try{stringBackupFile=@DataBaseOfBackupPath+@DataBaseOfBackupName;Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand();Comm.Connection=Conn;Comm.CommandText=usemaster;restoredatabase@DataBaseNameFromdisk=@BackupFilewithreplace;;Comm.Parameters.Add(newSqlParameter(@DataBaseName,SqlDbType.NVarChar));Comm.Parameters[@DataBaseName].Value=DataBaseName;Comm.Parameters.Add(newSqlParameter(@BackupFile,SqlDbType.NVarChar));Comm.Parameters[@BackupFile].Value=BackupFile;Comm.CommandType=CommandType.Text;Comm.ExecuteNonQuery();MyMessageBox.Show(还原数据库成功,信息提示);}catch(Exceptionex){MyMessageBox.Show(ex.Message,信息提示);}finally{Conn.Close();}}///summary///获取所以的数据库////summary///returns数据库/returnspublicListstringgetDatabase(){Liststringdatas=newListstring();stringsql=selectnamefromsysdatabases;try{Conn=newSqlConnection(ConnectionString);Conn.Open();Comm=newSqlCommand(sql,Conn);SqlDataReaderreader=Comm.ExecuteReader();while(reader.Read()){datas.Add(reader[0].ToString());}reader.Close();Conn.Close();}catch(Exception){thrownewException(数据库连接失败,请检查信息是否正确。);}returndatas;}}}