這個思路是在昨天寫了一個數據庫安裝類以后想及的,昨天稍微研究了一下自定義安裝的東西,感覺里面東西很多,以前我一直在找WISE和INSTALL SHIELD的FOR 。NET的程序,但現在覺得,對于一般應用而言,用。NET自己帶的工具是綽綽有余了。 我目前的想法是讓安裝程序除了建立虛擬目錄以外還得建立數據庫,省得用戶的手工操作,那樣就會避免很多麻煩。這個工作我昨天晚上在家的時候做了,今天我想我是不是應該能建立這樣的一個工具,能夠讓別的開發人員省掉這一步,包括導數據結構和建立安裝時候的數據庫設置窗體,我想用插件的形式做這個工作。 顯然BIGEAGLE知道我們大部分人的毛病,太浮躁了,不能把一件事情很專心的做好,于我而言,前輩的話是要聽的,毛病還是要犯的,所以我決定做完第一步以后剩下的事情就先不理了,:) 其實只是因為前些日子論壇上的一些事情,我希望能夠通過自己的努力讓更多人去關注自己應該關注的東西,去珍惜這個網上家園。 廢話說了這么多,:(
------------------------------------------------------------
文件SQLDMODemo.cs,實現操作的類
using System; using SQLDMO; using System.Windows.Forms;
namespace GenerateSQLScript { /// <summary> /// SQLDMODemo 的摘要說明。 /// </summary> public class SQLDMODemo { private const SQLDMO_SCRIPT_TYPE SQLDMOScript_Drops = SQLDMO_SCRIPT_TYPE.SQLDMOScript_Drops; private const SQLDMO_SCRIPT_TYPE SQLDMOScript_IncludeHeaders = SQLDMO_SCRIPT_TYPE.SQLDMOScript_IncludeHeaders; private const SQLDMO_SCRIPT_TYPE SQLDMOScript_Default = SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default; private const SQLDMO_SCRIPT_TYPE SQLDMOScript_AppendToFile =SQLDMO_SCRIPT_TYPE.SQLDMOScript_AppendToFile; private const SQLDMO_SCRIPT_TYPE SQLDMOScript_Bindings = SQLDMO_SCRIPT_TYPE.SQLDMOScript_Bindings;
private SQLDMO.SQLDMO_SCRIPT_TYPE intOptions;
private SQLDMO.SQLDMO_SCRIPT2_TYPE int2Options;
public SQLDMODemo() { // // TODO: 在此處添加構造函數邏輯 //
this.intOptions = SQLDMOScript_Drops | SQLDMOScript_IncludeHeaders | SQLDMOScript_Default | SQLDMOScript_AppendToFile | SQLDMOScript_Bindings; this.int2Options = SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default; }
/// <summary> /// 導出SCRIPT的函數 /// </summary> /// <param name="strServerName"></param> /// <param name="strUserName"></param> /// <param name="strPassword"></param> /// <param name="strDataBase"></param> /// <param name="strFilePath"></param>
public void GenerateSqlScript(string strServerName,string strUserName,string strPassword,string strDataBase,string strOwner,string strFilePath) { try {
SQLDMO.SQLServer sql = new SQLDMO.SQLServer(); SQLDMO.Database db = new SQLDMO.Database(); SQLDMO.Trigger trigger = new SQLDMO.Trigger();
//連接數據庫
sql.Connect(strServerName,strUserName,strPassword);
db = (SQLDMO.Database)sql.Databases.Item(strDataBase,strOwner);
//導出自定義類型
foreach (SQLDMO.UserDefinedDatatype objGen in db.UserDefinedDatatypes) { objGen.Script(intOptions,strFilePath,int2Options); }
//導出表和觸發器,過濾掉系統表
foreach (SQLDMO.Table objTable in db.Tables) { if (objTable.SystemObject == false) { objTable.Script(intOptions,strFilePath,null,int2Options);
foreach(SQLDMO.Trigger objTrigger in objTable.Triggers) { if (objTrigger.SystemObject == false) { objTrigger.Script(intOptions,strFilePath,int2Options); } } } }
//導出規則
foreach (SQLDMO.Rule objRule in db.Rules) { objRule.Script(intOptions,strFilePath,int2Options); }
//導出存儲過程
foreach (SQLDMO.StoredProcedure objProcedure in db.StoredProcedures) { if (objProcedure.SystemObject == false) { objProcedure.Script(intOptions,strFilePath,int2Options); } }
foreach (SQLDMO.View objView in db.Views) { if (objView.SystemObject == false) { objView.Script(intOptions,strFilePath,int2Options); } }
MessageBox.Show ("成功啦,恭喜,恭喜");
} catch(Exception e) { MessageBox.Show(e.Message); throw (e); } } } }
------------------------------------------------------------ 測試程序Form1.cs ------------------------------------------------------------
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace GenerateSQLScript { /// <summary> /// Form1 的摘要說明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗體設計器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // }
/// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(336, 128); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(568, 341); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
} #endregion
/// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void button1_Click(object sender, System.EventArgs e) { SQLDMODemo demo = new SQLDMODemo(); demo.GenerateSqlScript("(local)","sa","www.topcoolsite.com","bbs","dbo","c:\\aa.sql"); } } }
------------------------------------------------------------
感謝BigEagle、怡紅公子、開心就好、jh.mei在本人書寫本文時的幫助。
|