关于动态导出SQL数据库的SCRIPT

80酷酷网    80kuku.com

  动态|数据|数据库    这个思路是在昨天写了一个数据库安装类以后想及的,昨天稍微研究了一下自定义安装的东西,感觉里面东西很多,以前我一直在找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在本人书写本文时的帮助。

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: