如何用C#来部署数据库

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

现在好多程序,都是与数据库相关的,因此在做安装的时候,部署数据库看似是一件很复杂的事情。其实就我个人而言,部署数据库是很简单,大致的思路如下:
1.用本身的DBMS来产生数据库创建的SQL脚本;
2.接下来就是写程序来执行SQL脚本,从而达到创建数据库的目的。
 
以下用一个举例来说明,数据库服务器用的是SQLServer。
 
首先要在数据库生成好的SQL脚本最前头,加入如下语句:
      usemaster
GO
      
ifexists(select*fromsysdatabaseswherename='mytest')
           dropdatabasemytest
GO
      
createdatabasemytest
GO
      
usemytest
GO
注:其中“mytest”是要创建的数据库名。
 
而程序的代码如下:
//---------------------------CreateDB-------------------------------------


//-------------------------------------------------------------------------
//---File:frmCreateDB.cs
//---Description:ThemainformfiletocreatedatabaseusingspecificSQLfile
//---Author:Knight
//---Date:Mar.18,2006
//-------------------------------------------------------------------------
//-------------------------{CreateDB}-----------------------------------
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Data.SqlClient;
 
usingSystem.IO;
namespaceCreateDB
{
   ///<summary>
   ///SummarydescriptionforfrmCreateDB.
   ///</summary>
   publicclassfrmCreateDB:System.Windows.Forms.Form
   {
       privateSystem.Windows.Forms.Labellabel1;
       privateSystem.Windows.Forms.TextBoxtxtServerName;
       privateSystem.Windows.Forms.Labellabel2;
       privateSystem.Windows.Forms.Labellabel3;
       privateSystem.Windows.Forms.TextBoxtxtUserName;
       privateSystem.Windows.Forms.TextBoxtxtPassword;
       privateSystem.Windows.Forms.ButtonbtnCreateDB;
       ///<summary>
       ///Requireddesignervariable.
       ///</summary>
       privateSystem.ComponentModel.Containercomponents=null;
 
       publicfrmCreateDB()
       {
           //
           //RequiredforWindowsFormDesignersupport
           //
           InitializeComponent();
 
           //
           //TODO:AddanyconstructorcodeafterInitializeComponentcall
           //
       }
 
       ///<summary>
       ///Cleanupanyresourcesbeingused.
       ///</summary>
       protectedoverridevoidDispose(booldisposing)
       {
           if(disposing)
           {
               if(components!=null)
               {
                   components.Dispose();
               }
           }
           base.Dispose(disposing);
       }
 
       #regionWindowsFormDesignergeneratedcode
       ///<summary>
       ///RequiredmethodforDesignersupport-donotmodify
       ///thecontentsofthismethodwiththecodeeditor.
       ///</summary>
       privatevoidInitializeComponent()
       {
           this.label1=newSystem.Windows.Forms.Label();
           this.txtServerName=newSystem.Windows.Forms.TextBox();
           this.txtUserName=newSystem.Windows.Forms.TextBox();
           this.label2=newSystem.Windows.Forms.Label();
           this.txtPassword=newSystem.Windows.Forms.TextBox();
           this.label3=newSystem.Windows.Forms.Label();
           this.btnCreateDB=newSystem.Windows.Forms.Button();
           this.SuspendLayout();
           //
           //label1
           //
           this.label1.AutoSize=true;
           this.label1.Location=newSystem.Drawing.Point(32,32);
           this.label1.Name="label1";
           this.label1.Size=newSystem.Drawing.Size(74,16);
           this.label1.TabIndex=0;
           this.label1.Text="ServerName:";
           //
           //txtServerName
           //
           this.txtServerName.Location=newSystem.Drawing.Point(120,32);
           this.txtServerName.Name="txtServerName";
           this.txtServerName.Size=newSystem.Drawing.Size(152,20);
           this.txtServerName.TabIndex=1;
           this.txtServerName.Text="";
           //
           //txtUserName
           //
           this.txtUserName.Location=newSystem.Drawing.Point(120,64);
           this.txtUserName.Name="txtUserName";
           this.txtUserName.Size=newSystem.Drawing.Size(152,20);
           this.txtUserName.TabIndex=3;
           this.txtUserName.Text="";
           //
           //label2
           //
           this.label2.AutoSize=true;
           this.label2.Location=newSystem.Drawing.Point(40,64);
           this.label2.Name="label2";
           this.label2.Size=newSystem.Drawing.Size(64,16);
           this.label2.TabIndex=2;
           this.label2.Text="UserName:";
           //
           //txtPassword
           //
           this.txtPassword.Location=newSystem.Drawing.Point(120,96);
           this.txtPassword.Name="txtPassword";
           this.txtPassword.PasswordChar='*';
           this.txtPassword.Size=newSystem.Drawing.Size(152,20);
           this.txtPassword.TabIndex=5;
           this.txtPassword.Text="";
           //
           //label3
           //
           this.label3.AutoSize=true;
           this.label3.Location=newSystem.Drawing.Point(48,96);
           this.label3.Name="label3";
           this.label3.Size=newSystem.Drawing.Size(57,16);
           this.label3.TabIndex=4;
           this.label3.Text="Password:";
           //
           //btnCreateDB
           //
           this.btnCreateDB.Location=newSystem.Drawing.Point(168,136);
           this.btnCreateDB.Name="btnCreateDB";
           this.btnCreateDB.Size=newSystem.Drawing.Size(104,23);
           this.btnCreateDB.TabIndex=6;
           this.btnCreateDB.Text="&CreateDB";
           this.btnCreateDB.Click+=newSystem.EventHandler(this.btnCreateDB_Click);
           //
           //frmCreateDB
           //
           this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);
           this.ClientSize=newSystem.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="CreateDB";
           this.ResumeLayout(false);
 
       }
       #endregion
 
       ///<summary>
       ///Themainentrypointfortheapplication.
       ///</summary>
       [STAThread]
       staticvoidMain()
       {
           Application.Run(newfrmCreateDB());
       }
 
       privatevoidbtnCreateDB_Click(objectsender,System.EventArgse)
       {
           SqlConnectionsqlConn=newSqlConnection();

 try
               {
                   sqlComm.ExecuteNonQuery();
                   returntrue;
               }
               catch(SqlExceptionsqlErr)
               {
                   MessageBox.Show(sqlErr.Message);
               }
               catch
               {
               }
               
               sqlComm.Dispose();
           }
           returntrue;
       }
   }
}
 
      要注意的是在SQL脚本中的“/r/n”,在SQLCommand中是无法识别,因此要替换为空格;其次“GO”在SQLCommand中也是无法识别,但为了使每条语句都执行,因此我在这里,用“;”来替换。
 
      注:程序的位置和SQL脚本文件的位置为同一目录下,如果觉得不方便的话,可以在我的基础上再延伸。
       

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
上一篇PHP中模板分页的处理
下一篇超强分页类2.0发布
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭