ASP教程 |PHP教程 |JSP教程 |.net教程 |CGI教程 |XML教程 |AJAX |HTML |网站重构 |CSS教程 |JS教程 |网页设计 |数据库 |服务器 |开发工具 |网站运营
论坛模板 |CMS模板 |产品网页 |商务 |主机网站 |旅游网站 |体育 |娱乐 |艺术网站 |建筑网页 |动植食物 |人物网站 |教育网页 |企业网站 |简约 |另类
编程 |界面设计 |加解密 |浏览阅读 |装机必备 |IIS软件 |FTP软件 |安全软件 |远程监控 |邮件系统 |虚拟主机 |web服务 |组件 |设计软件 |数据库
网站运营 |asp电子书 |PHP电子书 |.net电子书 |JSP电子书 |CGI |数据库XML |服务器 |HTML |设计教程 |AJAX |C语言 |VB |DELPHI |安全 |其它
asp源码 |ASP.NET源码 |PHP源码 |JSP源码 |CGI源码 |FLASH源码 |AJAX源码 |电子商务 |办公OA源码 |公司网站源码 |整站源码
系统 |房屋 |苹果 |文件夹 |电脑 |影视 |动植物 |表情 |系列 |软件 |文件 |游戏 |工具 |CD光盘 |交通 |人物 |食物 |体育 |国旗 |卡通 |标志 |桌面
风景图片 |花草图片 |水资源图片 |动物 | 美食 |人物图片 |抽象 |民族艺术 |静物图片 |宇宙图片 |建筑图片 |装修图片 |科技交通 |体育图片 |精美图片
书法家字体 |迷你 |金梅 |汉仪 |华文 |长城 |创艺 |汉鼎 |金桥 |文鼎 |微软 |超世纪 |中国龙 |四通利方 |华康 |经典 |王汉宗 |中文 |英文 |其它
韩国酷站 |欧美酷站 |中国酷站 |日本酷站 |黑色酷站白色酷站 |灰色酷站 | 红色酷站 |橙色酷站 |黄色酷站 |绿色酷站 |青色酷站 |蓝色酷站 |紫色酷站
广告代码 |导航条 |菜单特效 |日期时间 |背景图像 |图层样式 |鼠标特效 |页面窗口 |色彩表格 |文字特效 |表单按钮 | 计数转换 |游戏特效 |实例特效
网站免费登陆 |Alexa排名查询 |广告代码下载 |站长工具 |查询工具 |技术手册查询 |WHOIS信息查询 |PR值查询 |收录查询 |极品万年历 |身份证查询
当前位置: 牛头网主页电脑学院.NET教程→ 如何用C#来部署数据库

如何用C#来部署数据库

来源:     作者:     时间:2008-08-17     点击:

现在好多程序,都是与数据库相关的,因此在做安装的时候,部署数据库看似是一件很复杂的事情。其实就我个人而言,部署数据库是很简单,大致的思路如下:
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脚本文件的位置为同一目录下,如果觉得不方便的话,可以在我的基础上再延伸。

0
顶一下
最新图文
相关文章
最新评论 共有 0 位网友发表了评论  查看所有评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:   匿名?  注册
牛头网
·
为站长提供最便捷的下载服务
本月热门
最近更新
| 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 诚聘英才 | 网站地图 | RSS订阅 | 建站服务 |