用C#来部署数据库

简介:

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

目录
相关文章
|
3月前
|
存储 监控 安全
数据库多实例的部署与配置方法
【10月更文挑战第23天】数据库多实例的部署和配置需要综合考虑多个因素,包括硬件资源、软件设置、性能优化、安全保障等。通过合理的部署和配置,可以充分发挥多实例的优势,提高数据库系统的运行效率和可靠性。在实际操作中,要不断总结经验,根据实际情况进行调整和优化,以适应不断变化的业务需求。
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
67 6
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue的班级综合测评管理系统设计与实现(系统源码+文档+数据库+部署等)
✌免费选题、功能需求设计、任务书、开题报告、中期检查、程序功能实现、论文辅导、论文降重、答辩PPT辅导、会议视频一对一讲解代码等✌
40 4
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生体质测试管理系统设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
38 2
|
26天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的冬奥会科普平台设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
45 0
|
2月前
|
SQL 弹性计算 安全
在云上轻松部署达梦数据库
达梦数据库(DM Database)是达梦数据库有限公司开发的关系型数据库管理系统,广泛应用于政府、金融、能源等行业。它具备高性能、高安全、兼容性强、易管理等特点,支持多种操作系统,适用于关键业务系统、政务系统及大数据处理等场景。在阿里云上,可通过一键部署快速使用达梦数据库DM8。
|
3月前
|
SQL 关系型数据库 数据库
国产数据实战之docker部署MyWebSQL数据库管理工具
【10月更文挑战第23天】国产数据实战之docker部署MyWebSQL数据库管理工具
290 4
国产数据实战之docker部署MyWebSQL数据库管理工具
|
3月前
|
PHP 数据库 数据安全/隐私保护
布谷直播源码部署服务器关于数据库配置的详细说明
布谷直播系统源码搭建部署时数据库配置明细!
|
3月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
|
3月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(下)
本文接续前文,深入讲解了在Windows环境下使用C#和ADO.NET操作南大通用GBase 8s数据库的方法。通过Visual Studio 2022创建项目,添加GBase 8s的DLL引用,并提供了详细的C#代码示例,涵盖数据库连接、表的创建与修改、数据的增删查改等操作,旨在帮助开发者提高数据库管理效率。