| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3265 人关注过本帖, 3 人收藏
标题:利用SQLDMO备份和还原数据库
只看楼主 加入收藏
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏(3)
 问题点数:0 回复次数:16 
利用SQLDMO备份和还原数据库


using System;
using System.Collections;
using SQLDMO;

namespace zj_SQLBackupAndRestore
{
/// <summary>
/// zj_SQLBackupAndRestore 的摘要说明。
/// </summary>
/// <creator>zj</creator>
/// <createDate>2006.12.15.AM</createDate>
public class zj_SQLBackupAndRestore
{

#region "构造函数"
/// <summary>
/// 不带参数的构造函数
/// </summary>
public zj_SQLBackupAndRestore()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion

#region "数据库参数返回"
/// <summary>
/// 返回服务器列表
/// </summary>
/// <returns>返回服务器列表的ArrayList对象</returns>
public static ArrayList SqlServerNameList()
{
try
{
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
for(int i=0;i<sqlServers.Count;i++)
{
object srvname = sqlServers.Item(i + 1);
if(srvname != null)
{
sqlServerName.Add(srvname);
}
}
return sqlServerName;
}
catch(Exception ex)
{
errorMessage=ex.Message;
return null;
}
}

/// <summary>
/// 数据库服务器连接
/// </summary>
/// <param name="sServer">服务器地址</param>
/// <param name="sUserName">数据库用户名</param>
/// <param name="sPwd">数据库密码</param>
/// <returns>true/false</returns>
public static bool SqlCon(string sServer,string sUserName,string sPwd)
{
sqlServer=sServer;
sqlUserName=sUserName;
sqlPwd=sPwd;
try
{
srv = new SQLDMO.SQLServerClass();
srv.Connect(sqlServer,sqlUserName,sqlPwd);
return true;
}
catch(Exception ex)
{
errorMessage=ex.Message;
return false;
}
}
/// <summary>
/// 返回数据库对象
/// </summary>
/// <returns>sqlTableName.ToArray()</returns>
public static ArrayList SqlDBNameList()
{
foreach(SQLDMO.Database db in srv.Databases)
{
if(db.Name!=null)
{
sqlDBName.Add(db.Name);
}
}
return sqlDBName;
}

/// <summary>
/// 返回数据库表对象
/// </summary>
/// <param name="sqlDataBase">数据库名</param>
/// <returns>返回数据库表对象</returns>
public static ArrayList SqlTableNameList(string sqlDataBase)
{
for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name ==sqlDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
sqlTableName.Clear();
for(int j=0;j<db.Tables.Count;j++)
{
sqlTableName.Add(db.Tables.Item(j+1,"dbo").Name);
}
}
}
return sqlTableName;
}

/// <summary>
/// 返回数据库存储过程对象
/// </summary>
/// <param name="sqlDataBase">数据库名</param>
/// <returns>返回数据库存储过程对象</returns>
public static ArrayList SqlStoredProceduresNameList(string sqlDataBase)
{
for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name ==sqlDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
sqlStoredProceduresName.Clear();
for(int j=0;j<db.StoredProcedures.Count;j++)
{
sqlStoredProceduresName.Add(db.StoredProcedures.Item(j+1,"dbo").Name);
}
}
}
return sqlStoredProceduresName;
}

/// <summary>
/// 数据库视图对象
/// </summary>
/// <param name="sqlDataBase">数据库名</param>
/// <returns>数据库视图对象</returns>
public static ArrayList SqlViewsNameList(string sqlDataBase)
{
for(int i=0;i<srv.Databases.Count;i++)
{
if(srv.Databases.Item(i+1,"dbo").Name ==sqlDataBase)
{
SQLDMO._Database db= srv.Databases.Item(i+1,"dbo");
sqlViewName.Clear();
for(int j=0;j<db.Views.Count;j++)
{
sqlViewName.Add(db.Views.Item(j+1,"dbo").Name);
}
}
}
return sqlViewName;
}
#endregion

#region "数据库备份"
/// <summary>
/// 备份数据库
/// </summary>
/// <param name="sServer">服务器名</param>
/// <param name="sUserName">用户名</param>
/// <param name="sPwd">密码</param>
/// <param name="BackUpDB">要备份的数据库名</param>
/// <param name="FilePath">备份文件存放路径</param>
/// <param name="BakcUpDBName">备份文件名</param>
/// <param name="TruncateLog">备份日志选项。其选项有:NoLog - 不备份交易日志/0。NoTruncate - 备份交易日志。日志里提供时间标记/1。Truncate - 备份交易日志,但不保留交易纪录/2。</param>
/// <returns>true/false</returns>
public static bool SqlBackUp(string sServer,string sUserName,string sPwd,string BackUpDB,string FilePath,string BakcUpDBName,TruncateLog TLog)
{
//实例对象
bkps=new SQLDMO.BackupClass();
if(SqlCon(sServer,sUserName,sPwd))
{
try
{
bkps.Database=BackUpDB; //指定需备份的数据库
bkps.Action = 0;
bkps.BackupSetName=BakcUpDBName;
bkps.Files=@FilePath +@"\" + BakcUpDBName; //指定备份文件路径
bkps.Initialize =true; //如设置为真(True),该备份装置将取代其他备份媒介而成为首选。
bkps.TruncateLog=(SQLDMO.SQLDMO_BACKUP_LOG_TYPE)TLog;
bkps.SQLBackup(srv);
bkps = null;
return true;
}
catch(Exception ex)
{
errorMessage=ex.Message;
errorMessage="";
return false;
}
finally
{
srv.DisConnect();
}
}
else
{
return false;
}
}

/// <summary>
/// 枚举备份日志类型
/// </summary>
public enum TruncateLog
{
NoLog=SQLDMO.SQLDMO_BACKUP_LOG_TYPE.SQLDMOBackup_Log_NoLog, //不备份交易日志
NoTruncate=SQLDMO.SQLDMO_BACKUP_LOG_TYPE.SQLDMOBackup_Log_NoTruncate, //备份交易日志。日志里提供时间标记
Truncate=SQLDMO.SQLDMO_BACKUP_LOG_TYPE.SQLDMOBackup_Log_Truncate //备份交易日志,但不保留交易纪录
}
#endregion

#region "数据库还原"
/// <summary>
/// 还原数据库
/// </summary>
/// <param name="sServer">服务器名</param>
/// <param name="sUserName">用户名</param>
/// <param name="sPwd">密码</param>
/// <param name="FilePath">要还原的数据库文件路径/param>
/// <param name="RestoreDBName">要还原的数据库名</param>
/// <returns>true/false</returns>
public static bool SQLRestoreDB(string sServer,string sUserName,string sPwd,string FilePath,string RestoreDBName)
{
oRestore = new SQLDMO.RestoreClass();
oRestore.Action = 0 ;
if(SqlCon(sServer,sUserName,sPwd))
{
try
{
SQLDMO.QueryResults qr = srv.EnumProcesses(-1) ;
int iColPIDNum = -1 ;
int iColDbName = -1 ;
//杀死其它的连接进程
for(int i=1;i<=qr.Columns;i++)
{
string strName = qr.get_ColumnName(i) ;
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i ;
}
else if (strName.ToUpper().Trim() == "DBNAME")
{
iColDbName = i ;
}
if (iColPIDNum != -1 && iColDbName != -1)
break ;
}
for(int i=1;i<=qr.Rows;i++)
{
int lPID = qr.GetColumnLong(i,iColPIDNum) ;
string strDBName = qr.GetColumnString(i,iColDbName) ;
if (strDBName.ToUpper() == "CgRecord".ToUpper())
srv.KillProcess(lPID) ;
}
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = RestoreDBName;
oRestore.Files = @FilePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(srv);
return true;
}
catch(System.Exception ex)
{
errorMessage=ex.Message;
return false;
}
finally
{
srv.DisConnect();
}
}
else
{
return false;
}
}
#endregion

#region "内部函数"
/// <summary>
/// 服务器地址
/// </summary>
private static object sqlServer;
/// <summary>
/// 数据库用户名
/// </summary>
private static object sqlUserName;
/// <summary>
/// 数据库密码
/// </summary>
private static object sqlPwd;
/// <summary>
/// 数据库备份对象
/// </summary>
private static SQLDMO.Backup bkps;
/// <summary>
/// 数据库还原对象
/// </summary>
private static SQLDMO.Restore oRestore;
/// <summary>
/// 数据库连接对象
/// </summary>
private static SQLDMO.SQLServer srv;
private static SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass() ;
/// <summary>
/// 错误消息
/// </summary>
private static string errorMessage;
/// <summary>
/// 服务器对象
/// </summary>
private static ArrayList sqlServerName=new ArrayList();
/// <summary>
/// 数据库表对象
/// </summary>
private static ArrayList sqlTableName=new ArrayList();
/// <summary>
/// 数据库对象
/// </summary>
private static ArrayList sqlDBName=new ArrayList();
/// <summary>
/// 数据库存储过程对象
/// </summary>
private static ArrayList sqlStoredProceduresName=new ArrayList();
/// <summary>
/// 数据库视图对象
/// </summary>
private static ArrayList sqlViewName=new ArrayList();
#endregion

}
}

搜索更多相关主题的帖子: 数据库 SQLDMO summary using creator 
2007-02-28 14:50
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
顶!!好东西
谢谢!

海鸽 is My Lover!!
2007-02-28 15:03
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 

的确好东西
学习学习。


有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-02-28 15:05
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 

1、先将上面的代码编译成SQLBackupAndRestore组件。
2、然后创建一个WindowsApplication项目,将下面代码保存为SqlProxyMessager.cs文件,

using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using zj_SQLBackupAndRestore;
using System.Threading;

namespace SqlWorkProxy
{
/// <summary>
/// SqlProxyMessager 的摘要说明。
/// </summary>
public class SqlProxyMessager
{
/// <summary>
/// SqlWorkProxyForm窗体对象
/// </summary>
private SqlWorkProxy.SqlWorkProxyForm swp;
/// <summary>
/// 文件另存为对象
/// </summary>
private SaveFileDialog mySave;
/// <summary>
/// 文件打开对象
/// </summary>
private OpenFileDialog myOpen;
/// <summary>
/// 文件夹浏览对象
/// </summary>
private FolderBrowserDialog myOpenPath;
/// <summary>
/// 数据库服务器名
/// </summary>
private ArrayList ServerName;
/// <summary>
/// 数据库名
/// </summary>
private ArrayList SQLDBName;
private int i;
private static string SQLServer;
private static string SQLUserName;
private static string SQLPassWord;
private static string SQLDB;
/// <summary>
/// 备份日志类型
/// </summary>
private static object LogType;
public SqlProxyMessager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public void DisPlayMessager()
{
swp=new SqlWorkProxy.SqlWorkProxyForm();
swp.btnSaveFile.Click +=new EventHandler(btnSaveFile_Click);
swp.btnOpenFile.Click +=new EventHandler(btnOpenFile_Click);
swp.Closed +=new EventHandler(swp_Closed);
swp.Load +=new EventHandler(swp_Load);
swp.btnConnect.Click +=new EventHandler(btnConnect_Click);
swp.btnNoConnect.Click +=new EventHandler(btnNoConnect_Click);
swp.btnBackUpDB.Click +=new EventHandler(btnBackUpDB_Click);
swp.cmbSQL.SelectedValueChanged +=new EventHandler(cmbSQL_SelectedValueChanged);
swp.btnRestoreDB.Click +=new EventHandler(btnRestoreDB_Click);
swp.timer1.Tick +=new EventHandler(timer1_Tick);
swp.ShowDialog();
}

/// <summary>
/// 还原文件路径及文件名
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenFile_Click(object sender, EventArgs e)
{
myOpen=new OpenFileDialog();
myOpen.Filter="备份文件(*.BAK)|*.bak|所有文件(*.*)|*.*";
if(myOpen.ShowDialog()==DialogResult.OK)
{
swp.tbRestoreFileName.Text=myOpen.FileName.ToString();
string FileName=myOpen.FileName.Split('\\')[myOpen.FileName.Split('\\').Length-1];
swp.tbRestoreDBName.Text=FileName.Split('.')[0].ToString();
}
}

/// <summary>
/// 备份文件路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSaveFile_Click(object sender, EventArgs e)
{
myOpenPath=new FolderBrowserDialog();
if(myOpenPath.ShowDialog()==DialogResult.OK)
{
swp.tbBackUpPath.Text=myOpenPath.SelectedPath.ToString();
}
}

/// <summary>
/// 窗体关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void swp_Closed(object sender, EventArgs e)
{
foreach(System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
{
if(process.ProcessName.ToUpper().Equals("SQLWORKPROXY"))
{
process.Kill();
}
}
}

/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void swp_Load(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
// ServerName=new ArrayList();
// ServerName=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlServerNameList();
// if(ServerName!=null)
// {
// for(i=0;i<ServerName.Count;i++)
// {
// swp.cmbServer.Items.Add(ServerName[i].ToString());
// swp.cmbServer.Text=ServerName[0].ToString();
// }
// }
// else
// {
// swp.cmbServer.Text=string.Empty;
// }
swp.timer1.Start();
}

private void ThreadProc()
{
ServerName=new ArrayList();
ServerName=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlServerNameList();
if(ServerName!=null)
{
for(i=0;i<ServerName.Count;i++)
{
swp.cmbServer.Items.Add(ServerName[i].ToString());
swp.cmbServer.Text=ServerName[0].ToString();
}
}
else
{
swp.cmbServer.Text=string.Empty;
}
}

/// <summary>
/// 连接数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
swp.cmbSQL.Items.Clear();
if(zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlCon(swp.cmbServer.Text,swp.tbUserName.Text,swp.tbPassWord.Text.Trim()))
{
SQLDBName=new ArrayList();
SQLDBName.Clear();
SQLDBName=BindDBName();
if(SQLDBName!=null)
{
for(i=0;i<SQLDBName.Count;i++)
{
swp.cmbSQL.Items.Add(SQLDBName[i].ToString());
swp.cmbSQL.Text=SQLDBName[0].ToString();
swp.tbBackUpDBName.Text=SQLDBName[0].ToString()+DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString();
SQLDB=swp.cmbSQL.Text;
}
}
else
{
swp.cmbSQL.Text=string.Empty;
}
swp.statusBarPanel1.Text="数据库连接成功。";
SQLServer=swp.cmbServer.Text;
SQLUserName=swp.tbUserName.Text;
SQLPassWord=swp.tbPassWord.Text.Trim();
}
else
{
swp.statusBarPanel1.Text="数据库不存在或拒绝被访问!请确认后重新连接。";
}
}

/// <summary>
/// 获取数据库名
/// </summary>
/// <returns></returns>
private ArrayList BindDBName()
{
SQLDBName=new ArrayList();
SQLDBName.Clear();
SQLDBName=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlDBNameList();
return SQLDBName;
}
/// <summary>
/// 断开数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNoConnect_Click(object sender, EventArgs e)
{
if(!zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlCon(string.Empty,string.Empty,string.Empty))
{
swp.statusBarPanel1.Text="数据库已经断开连接。";
}
}

/// <summary>
/// 备份数据文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBackUpDB_Click(object sender, EventArgs e)
{
if(swp.tbBackUpPath.Text!=null)
{
if(zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SqlBackUp(SQLServer,SQLUserName,SQLPassWord,SQLDB,swp.tbBackUpPath.Text,swp.tbBackUpDBName.Text,(zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.TruncateLog)BackUpType()))
{
swp.statusBarPanel1.Text="数据库备份成功。";
}
}
else
{
MessageBox.Show("请选择数据库备份路径!","系统提示");
}
}

/// <summary>
/// 选择不同类型备份返回不同的对象
/// </summary>
/// <returns></returns>
private object BackUpType()
{
if(swp.rbtnNoLog.Checked)
{
LogType=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.TruncateLog.NoLog;
}
if(swp.rbtnLog.Checked)
{
LogType=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.TruncateLog.Truncate;
}
if(swp.rbtnLogDate.Checked)
{
LogType=zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.TruncateLog.NoTruncate;
}
return LogType;
}

/// <summary>
/// 数据库发生改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbSQL_SelectedValueChanged(object sender, EventArgs e)
{
SQLDB=swp.cmbSQL.Text;
swp.tbBackUpDBName.Text=swp.cmbSQL.Text.ToString()+DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString();
}

/// <summary>
/// 数据库还原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRestoreDB_Click(object sender, EventArgs e)
{
if(swp.tbRestoreFileName.Text!=null)
{
if(swp.tbRestoreDBName.Text!=null)
{
if(zj_SQLBackupAndRestore.zj_SQLBackupAndRestore.SQLRestoreDB(SQLServer,SQLUserName,SQLPassWord,swp.tbRestoreFileName.Text,swp.tbRestoreDBName.Text))
{
swp.statusBarPanel1.Text="数据库还原成功。";
}
}
else
{
MessageBox.Show("请输入还原数据库名!","系统提示");
}
}
else
{
MessageBox.Show("请选择数据库还原文件路径!","系统提示");
}
}

private void timer1_Tick(object sender, EventArgs e)
{
swp.statusBarPanel3.Text=DateTime.Now.ToString();
}
}
}



等待相逢
2007-02-28 15:15
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 

3、然后创建一个窗体,将下面代码COPY进去
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SqlWorkProxy
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class SqlWorkProxyForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
internal System.Windows.Forms.Button btnNoConnect;
internal System.Windows.Forms.Button btnConnect;
internal System.Windows.Forms.TextBox tbPassWord;
internal System.Windows.Forms.TextBox tbUserName;
internal System.Windows.Forms.ComboBox cmbServer;
internal System.Windows.Forms.RadioButton rbtnLog;
internal System.Windows.Forms.RadioButton rbtnLogDate;
internal System.Windows.Forms.Button btnBackUpDB;
internal System.Windows.Forms.TextBox tbBackUpDBName;
internal System.Windows.Forms.TextBox tbBackUpPath;
internal System.Windows.Forms.ComboBox cmbSQL;
internal System.Windows.Forms.Button btnRestoreDB;
internal System.Windows.Forms.TextBox tbRestoreDBName;
internal System.Windows.Forms.TextBox tbRestoreFileName;
internal System.Windows.Forms.OpenFileDialog openRestoreFile;
internal System.Windows.Forms.SaveFileDialog saveBackUpFile;
internal System.Windows.Forms.Button btnSaveFile;
internal System.Windows.Forms.Button btnOpenFile;
private System.Windows.Forms.FolderBrowserDialog folderBackUpFilePath;
internal System.Windows.Forms.StatusBar statusBar1;
internal System.Windows.Forms.StatusBarPanel statusBarPanel1;
internal System.Windows.Forms.StatusBarPanel statusBarPanel2;
internal System.Windows.Forms.StatusBarPanel statusBarPanel3;
internal System.Windows.Forms.RadioButton rbtnNoLog;
internal System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

public SqlWorkProxyForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();


//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.splitter1 = new System.Windows.Forms.Splitter();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnNoConnect = new System.Windows.Forms.Button();
this.btnConnect = new System.Windows.Forms.Button();
this.tbPassWord = new System.Windows.Forms.TextBox();
this.tbUserName = new System.Windows.Forms.TextBox();
this.cmbServer = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.rbtnLog = new System.Windows.Forms.RadioButton();
this.rbtnLogDate = new System.Windows.Forms.RadioButton();
this.rbtnNoLog = new System.Windows.Forms.RadioButton();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.btnBackUpDB = new System.Windows.Forms.Button();
this.btnSaveFile = new System.Windows.Forms.Button();
this.tbBackUpDBName = new System.Windows.Forms.TextBox();
this.tbBackUpPath = new System.Windows.Forms.TextBox();
this.cmbSQL = new System.Windows.Forms.ComboBox();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.btnRestoreDB = new System.Windows.Forms.Button();
this.label13 = new System.Windows.Forms.Label();
this.tbRestoreDBName = new System.Windows.Forms.TextBox();
this.btnOpenFile = new System.Windows.Forms.Button();
this.tbRestoreFileName = new System.Windows.Forms.TextBox();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.openRestoreFile = new System.Windows.Forms.OpenFileDialog();
this.saveBackUpFile = new System.Windows.Forms.SaveFileDialog();
this.folderBackUpFilePath = new System.Windows.Forms.FolderBrowserDialog();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit();
this.SuspendLayout();
//
// splitter1
//
this.splitter1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.splitter1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(592, 32);
this.splitter1.TabIndex = 3;
this.splitter1.TabStop = false;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Font = new System.Drawing.Font("楷体_GB2312", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 16);
this.label1.TabIndex = 4;
this.label1.Text = "数据库备份与还原";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnNoConnect);
this.groupBox1.Controls.Add(this.btnConnect);
this.groupBox1.Controls.Add(this.tbPassWord);
this.groupBox1.Controls.Add(this.tbUserName);
this.groupBox1.Controls.Add(this.cmbServer);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Location = new System.Drawing.Point(8, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(576, 112);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "连接数据库服务器";
//
// btnNoConnect
//
this.btnNoConnect.Location = new System.Drawing.Point(360, 80);
this.btnNoConnect.Name = "btnNoConnect";
this.btnNoConnect.TabIndex = 7;
this.btnNoConnect.Text = "断开";
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(360, 48);
this.btnConnect.Name = "btnConnect";
this.btnConnect.TabIndex = 6;
this.btnConnect.Text = "连接";
//
// tbPassWord
//
this.tbPassWord.Location = new System.Drawing.Point(152, 80);
this.tbPassWord.Name = "tbPassWord";
this.tbPassWord.PasswordChar = '*';
this.tbPassWord.Size = new System.Drawing.Size(160, 21);
this.tbPassWord.TabIndex = 5;
this.tbPassWord.Text = "";
//
// tbUserName
//
this.tbUserName.Location = new System.Drawing.Point(152, 48);
this.tbUserName.Name = "tbUserName";
this.tbUserName.Size = new System.Drawing.Size(160, 21);
this.tbUserName.TabIndex = 4;
this.tbUserName.Text = "";
//
// cmbServer
//
this.cmbServer.Location = new System.Drawing.Point(152, 20);
this.cmbServer.Name = "cmbServer";
this.cmbServer.Size = new System.Drawing.Size(272, 20);
this.cmbServer.TabIndex = 3;
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 85);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 16);
this.label4.TabIndex = 2;
this.label4.Text = "服务器密码:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 53);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 1;
this.label3.Text = "服务器用户名:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 0;
this.label2.Text = "选择服务器:";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.rbtnLog);
this.groupBox2.Controls.Add(this.rbtnLogDate);
this.groupBox2.Controls.Add(this.rbtnNoLog);
this.groupBox2.Controls.Add(this.label10);
this.groupBox2.Controls.Add(this.label9);
this.groupBox2.Controls.Add(this.btnBackUpDB);
this.groupBox2.Controls.Add(this.btnSaveFile);
this.groupBox2.Controls.Add(this.tbBackUpDBName);
this.groupBox2.Controls.Add(this.tbBackUpPath);
this.groupBox2.Controls.Add(this.cmbSQL);
this.groupBox2.Controls.Add(this.label8);
this.groupBox2.Controls.Add(this.label7);
this.groupBox2.Controls.Add(this.label6);
this.groupBox2.Location = new System.Drawing.Point(8, 168);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(576, 144);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "数据库备份";
//
// rbtnLog
//
this.rbtnLog.Location = new System.Drawing.Point(400, 48);
this.rbtnLog.Name = "rbtnLog";
this.rbtnLog.Size = new System.Drawing.Size(152, 24);
this.rbtnLog.TabIndex = 12;
this.rbtnLog.Text = "备份不带日期交易日志";
//
// rbtnLogDate
//
this.rbtnLogDate.Location = new System.Drawing.Point(264, 48);
this.rbtnLogDate.Name = "rbtnLogDate";
this.rbtnLogDate.Size = new System.Drawing.Size(136, 24);
this.rbtnLogDate.TabIndex = 11;
this.rbtnLogDate.Text = "备份带日期交易日志";
//
// rbtnNoLog
//
this.rbtnNoLog.Checked = true;
this.rbtnNoLog.Location = new System.Drawing.Point(152, 48);
this.rbtnNoLog.Name = "rbtnNoLog";
this.rbtnNoLog.Size = new System.Drawing.Size(112, 24);
this.rbtnNoLog.TabIndex = 10;
this.rbtnNoLog.TabStop = true;
this.rbtnNoLog.Text = "不备份交易日志";
//
// label10
//
this.label10.Location = new System.Drawing.Point(16, 56);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(100, 16);
this.label10.TabIndex = 9;
this.label10.Text = "日志备份:";
//
// label9
//
this.label9.ForeColor = System.Drawing.Color.Red;
this.label9.Location = new System.Drawing.Point(312, 120);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(160, 16);
this.label9.TabIndex = 8;
this.label9.Text = "默认为数据库名加系统日期";


等待相逢
2007-02-28 15:17
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 

续上
//
// btnBackUpDB
//
this.btnBackUpDB.Location = new System.Drawing.Point(480, 113);
this.btnBackUpDB.Name = "btnBackUpDB";
this.btnBackUpDB.TabIndex = 7;
this.btnBackUpDB.Text = "备份数据库";
//
// btnSaveFile
//
this.btnSaveFile.Location = new System.Drawing.Point(392, 81);
this.btnSaveFile.Name = "btnSaveFile";
this.btnSaveFile.Size = new System.Drawing.Size(40, 23);
this.btnSaveFile.TabIndex = 6;
this.btnSaveFile.Text = "浏览";
//
// tbBackUpDBName
//
this.tbBackUpDBName.Location = new System.Drawing.Point(152, 115);
this.tbBackUpDBName.Name = "tbBackUpDBName";
this.tbBackUpDBName.Size = new System.Drawing.Size(160, 21);
this.tbBackUpDBName.TabIndex = 5;
this.tbBackUpDBName.Text = "";
//
// tbBackUpPath
//
this.tbBackUpPath.BackColor = System.Drawing.SystemColors.HighlightText;
this.tbBackUpPath.Enabled = false;
this.tbBackUpPath.Location = new System.Drawing.Point(152, 83);
this.tbBackUpPath.Name = "tbBackUpPath";
this.tbBackUpPath.Size = new System.Drawing.Size(240, 21);
this.tbBackUpPath.TabIndex = 4;
this.tbBackUpPath.Text = "";
//
// cmbSQL
//
this.cmbSQL.Location = new System.Drawing.Point(152, 20);
this.cmbSQL.Name = "cmbSQL";
this.cmbSQL.Size = new System.Drawing.Size(280, 20);
this.cmbSQL.TabIndex = 3;
//
// label8
//
this.label8.Location = new System.Drawing.Point(16, 120);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(100, 16);
this.label8.TabIndex = 2;
this.label8.Text = "备份文件名:";
//
// label7
//
this.label7.Location = new System.Drawing.Point(16, 88);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(100, 16);
this.label7.TabIndex = 1;
this.label7.Text = "选择备份路径:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(128, 16);
this.label6.TabIndex = 0;
this.label6.Text = "选择要备份的数据库:";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.btnRestoreDB);
this.groupBox3.Controls.Add(this.label13);
this.groupBox3.Controls.Add(this.tbRestoreDBName);
this.groupBox3.Controls.Add(this.btnOpenFile);
this.groupBox3.Controls.Add(this.tbRestoreFileName);
this.groupBox3.Controls.Add(this.label12);
this.groupBox3.Controls.Add(this.label11);
this.groupBox3.Location = new System.Drawing.Point(8, 320);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(576, 88);
this.groupBox3.TabIndex = 7;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "数据库还原";
//
// btnRestoreDB
//
this.btnRestoreDB.Location = new System.Drawing.Point(480, 48);
this.btnRestoreDB.Name = "btnRestoreDB";
this.btnRestoreDB.TabIndex = 10;
this.btnRestoreDB.Text = "还原数据库";
//
// label13
//
this.label13.ForeColor = System.Drawing.Color.Red;
this.label13.Location = new System.Drawing.Point(320, 56);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(104, 16);
this.label13.TabIndex = 9;
this.label13.Text = "默认为备份文件名";
//
// tbRestoreDBName
//
this.tbRestoreDBName.Location = new System.Drawing.Point(152, 51);
this.tbRestoreDBName.Name = "tbRestoreDBName";
this.tbRestoreDBName.Size = new System.Drawing.Size(160, 21);
this.tbRestoreDBName.TabIndex = 8;
this.tbRestoreDBName.Text = "";
//
// btnOpenFile
//
this.btnOpenFile.Location = new System.Drawing.Point(392, 17);
this.btnOpenFile.Name = "btnOpenFile";
this.btnOpenFile.Size = new System.Drawing.Size(40, 23);
this.btnOpenFile.TabIndex = 7;
this.btnOpenFile.Text = "浏览";
//
// tbRestoreFileName
//
this.tbRestoreFileName.BackColor = System.Drawing.SystemColors.HighlightText;
this.tbRestoreFileName.Enabled = false;
this.tbRestoreFileName.Location = new System.Drawing.Point(152, 19);
this.tbRestoreFileName.Name = "tbRestoreFileName";
this.tbRestoreFileName.Size = new System.Drawing.Size(240, 21);
this.tbRestoreFileName.TabIndex = 5;
this.tbRestoreFileName.Text = "";
//
// label12
//
this.label12.Location = new System.Drawing.Point(16, 56);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(112, 16);
this.label12.TabIndex = 1;
this.label12.Text = "还原数据库名称:";
//
// label11
//
this.label11.Location = new System.Drawing.Point(16, 24);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(100, 16);
this.label11.TabIndex = 0;
this.label11.Text = "选择备份文件:";
//
// saveBackUpFile
//
this.saveBackUpFile.AddExtension = false;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 414);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2,
this.statusBarPanel3});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(592, 24);
this.statusBar1.TabIndex = 9;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Width = 300;
//
// statusBarPanel2
//
this.statusBarPanel2.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
this.statusBarPanel2.Width = 150;
//
// statusBarPanel3
//
this.statusBarPanel3.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
this.statusBarPanel3.Width = 142;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
//
// SqlWorkProxyForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 438);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.splitter1);
this.Name = "SqlWorkProxyForm";
this.Text = "数据库代理";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).EndInit();
this.ResumeLayout(false);

}
#endregion

}
}


等待相逢
2007-02-28 15:17
梓风
Rank: 2
等 级:新手上路
威 望:4
帖 子:181
专家分:0
注 册:2006-10-22
收藏
得分:0 

4、在创建一个窗体,将下面的代码写入。
using System;
using System.Windows.Forms;

namespace SqlWorkProxy
{
/// <summary>
/// AppContext 的摘要说明。
/// </summary>
public class AppContext:System.Windows.Forms.Form
{
public AppContext()
{
SqlProxyMessager spm=new SqlProxyMessager();
spm.DisPlayMessager();
}

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new AppContext());
}
}
}

在这里我之所以要用另一个窗体来调用,这个例子实现了代码和窗体文件完全的分离,这样就能将几个完成同一个功能模块的窗体的代码写到一个类里面去,这个要大家自己去体会。我只是给一个例子大家看看,希望能有所收获。


等待相逢
2007-02-28 15:21
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
辛苦了,楼主!
说得比较详细啊

海鸽 is My Lover!!
2007-02-28 15:41
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
收藏
得分:0 

SQLDMO,我没用过,谢谢楼主


26403021 sql群 博客 blog./user15/81152/index.shtml
2007-02-28 15:45
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 
我手都复制软了

有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-02-28 16:27
快速回复:利用SQLDMO备份和还原数据库
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067118 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved