天天看点

用C#来部署数据库

现在好多程序,都是与数据库相关的,因此在做安装的时候,部署数据库看似是一件很复杂的事情。其实就我个人而言,部署数据库是很简单,大致的思路如下:

1. 用本身的dbms来产生数据库创建的sql脚本;

2. 接下来就是写程序来执行sql脚本,从而达到创建数据库的目的。

以下用一个举例来说明,数据库服务器用的是sql server。

首先要在数据库生成好的sql脚本最前头,加入如下语句:

       use master

go

if exists (select * from sysdatabases where name='mytest')

            drop database mytest

create database mytest

use mytest

注:其中“mytest”是要创建的数据库名。

而程序的代码如下:

//---------------------------create db-------------------------------------

//-------------------------------------------------------------------------

//---file:frmcreatedb.cs

//---description:the main form file to create database using specific sql file

//---author:knight

//---date:mar.18, 2006

//-------------------------{ create db }-----------------------------------

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using system.data.sqlclient;

using system.io;

namespace createdb

{

    ///<summary>

    /// summary description for frmcreatedb.

    ///</summary>

    public class frmcreatedb : system.windows.forms.form

    {

        private system.windows.forms.label label1;

        private system.windows.forms.textbox txtservername;

        private system.windows.forms.label label2;

        private system.windows.forms.label label3;

        private system.windows.forms.textbox txtusername;

        private system.windows.forms.textbox txtpassword;

        private system.windows.forms.button btncreatedb;

        ///<summary>

        /// required designer variable.

        ///</summary>

        private system.componentmodel.container components = null;

        public frmcreatedb()

        {

            //

            // required for windows form designer support

            initializecomponent();

            // todo: add any constructor code after initializecomponent call

        }

        /// clean up any resources being used.

        protected override void dispose( bool disposing )

            if( disposing )

            {

                if (components != null)

                {

                    components.dispose();

                }

            }

            base.dispose( disposing );

        #region windows form designer generated code

        /// required method for designer support - do not modify

        /// the contents of this method with the code editor.

        private void initializecomponent()

            this.label1 = new system.windows.forms.label();

            this.txtservername = new system.windows.forms.textbox();

            this.txtusername = new system.windows.forms.textbox();

            this.label2 = new system.windows.forms.label();

            this.txtpassword = new system.windows.forms.textbox();

            this.label3 = new system.windows.forms.label();

            this.btncreatedb = new system.windows.forms.button();

            this.suspendlayout();

            // label1

            this.label1.autosize = true;

            this.label1.location = new system.drawing.point(32, 32);

            this.label1.name = "label1";

            this.label1.size = new system.drawing.size(74, 16);

            this.label1.tabindex = 0;

            this.label1.text = "server name:";

            // txtservername

            this.txtservername.location = new system.drawing.point(120, 32);

            this.txtservername.name = "txtservername";

            this.txtservername.size = new system.drawing.size(152, 20);

            this.txtservername.tabindex = 1;

            this.txtservername.text = "";

            // txtusername

            this.txtusername.location = new system.drawing.point(120, 64);

            this.txtusername.name = "txtusername";

            this.txtusername.size = new system.drawing.size(152, 20);

            this.txtusername.tabindex = 3;

            this.txtusername.text = "";

            // label2

            this.label2.autosize = true;

            this.label2.location = new system.drawing.point(40, 64);

            this.label2.name = "label2";

            this.label2.size = new system.drawing.size(64, 16);

            this.label2.tabindex = 2;

            this.label2.text = "user name:";

            // txtpassword

            this.txtpassword.location = new system.drawing.point(120, 96);

            this.txtpassword.name = "txtpassword";

            this.txtpassword.passwordchar = '*';

            this.txtpassword.size = new system.drawing.size(152, 20);

            this.txtpassword.tabindex = 5;

            this.txtpassword.text = "";

            // label3

            this.label3.autosize = true;

            this.label3.location = new system.drawing.point(48, 96);

            this.label3.name = "label3";

            this.label3.size = new system.drawing.size(57, 16);

            this.label3.tabindex = 4;

            this.label3.text = "password:";

            // btncreatedb

            this.btncreatedb.location = new system.drawing.point(168, 136);

            this.btncreatedb.name = "btncreatedb";

            this.btncreatedb.size = new system.drawing.size(104, 23);

            this.btncreatedb.tabindex = 6;

            this.btncreatedb.text = "&create db";

            this.btncreatedb.click += new system.eventhandler(this.btncreatedb_click);

            // frmcreatedb

            this.autoscalebasesize = new system.drawing.size(5, 13);

            this.clientsize = new system.drawing.size(306, 175);

            this.controls.add(this.btncreatedb);

            this.controls.add(this.txtpassword);

            this.controls.add(this.label3);

            this.controls.add(this.txtusername);

            this.controls.add(this.label2);

            this.controls.add(this.txtservername);

            this.controls.add(this.label1);

            this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;

            this.maximizebox = false;

            this.name = "frmcreatedb";

            this.startposition = system.windows.forms.formstartposition.centerscreen;

            this.text = "create db";

            this.resumelayout(false);

        #endregion

        /// the main entry point for the application.

        [stathread]

        static void main()

            application.run(new frmcreatedb());

        private void btncreatedb_click(object sender, system.eventargs e)

            sqlconnection sqlconn = new sqlconnection();

  try

                    sqlcomm.executenonquery();

                    return true;

                catch( sqlexception sqlerr )

                    messagebox.show( sqlerr.message );

                catch

                sqlcomm.dispose();

            return true;

    }

}

       要注意的是在sql脚本中的“/r/n”,在sqlcommand中是无法识别,因此要替换为空格;其次“go” 在sqlcommand中也是无法识别,但为了使每条语句都执行,因此我在这里,用“;”来替换。

       注:程序的位置和sql脚本文件的位置为同一目录下,如果觉得不方便的话,可以在我的基础上再延伸。