国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何用C#來部署數據庫

2024-07-21 02:30:18
字體:
來源:轉載
供稿:網友

現在好多程序,都是與數據庫相關的,因此在做安裝的時候,部署數據庫看似是一件很復雜的事情。其實就我個人而言,部署數據庫是很簡單,大致的思路如下:
1. 用本身的dbms來產生數據庫創建的sql腳本;
2. 接下來就是寫程序來執行sql腳本,從而達到創建數據庫的目的。
 
以下用一個舉例來說明,數據庫服務器用的是sql server。
 
首先要在數據庫生成好的sql腳本最前頭,加入如下語句:
       use master
go
      
if exists (select * from sysdatabases where name='mytest')
            drop database mytest
go
      
create database mytest
go
      
use mytest
go
注:其中“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
            //
        }
 
        ///<summary>
        /// clean up any resources being used.
        ///</summary>
        protected override void dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.dispose();
                }
            }
            base.dispose( disposing );
        }
 
        #region windows form designer generated code
        ///<summary>
        /// required method for designer support - do not modify
        /// the contents of this method with the code editor.
        ///</summary>
        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
 
        ///<summary>
        /// the main entry point for the application.
        ///</summary>
        [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腳本文件的位置為同一目錄下,如果覺得不方便的話,可以在我的基礎上再延伸。
       

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德安县| 丹巴县| 西华县| 陆良县| 白银市| 略阳县| 磐石市| 巴楚县| 桃江县| 丰镇市| 石景山区| 迁西县| 五常市| 星子县| 彭山县| 本溪市| 徐水县| 广宗县| 台州市| 临湘市| 莱阳市| 房山区| 疏附县| 清远市| 安龙县| 叶城县| 金坛市| 会泽县| 丹寨县| 安多县| 安吉县| 天气| 民权县| 五常市| 台北县| 民县| 明星| 镶黄旗| 东乌珠穆沁旗| 杭锦后旗| 海伦市|