用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月前
|
存储 关系型数据库 数据库
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
本文通过一个 Agentic RAG 应用的完整构建流程,展示了如何借助 RDS Supabase 快速搭建具备知识处理与智能决策能力的 AI 应用,展示从数据准备到应用部署的全流程,相较于传统开发模式效率大幅提升。
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
|
3月前
|
数据可视化 BI API
无缝对接云数据库:自定义报表生成工具在混合云环境下的部署指南
自定义报表生成工具通过拖拽设计、多数据源整合及自动化输出,帮助业务人员零代码创建个性化报表,解决传统工具灵活性不足、技术门槛高的问题。文章对比其与传统报表差异,列举行业应用场景(如财务、零售),并给出选型建议与主流工具(如FineReport、Power BI、板栗看板)的优劣势分析。
119 0
|
7月前
|
SQL 关系型数据库 数据库
【YashanDB知识库】OM仲裁节点故障后手工切换方案和yasom仲裁重新部署后重新纳管数据库集群方案
本文介绍了主备数据库集群的部署、OM仲裁故障切换及重新纳管的全过程。首先通过解压软件包并调整安装参数完成数据库集群部署,接着说明了在OM仲裁故障时的手动切换方案,包括关闭自动切换开关、登录备节点执行切换命令。最后详细描述了搭建新的yasom仲裁节点以重新纳管数据库集群的步骤,如生成配置文件、初始化进程、执行托管命令等,确保新旧系统无缝衔接,保障数据服务稳定性。
|
7月前
|
数据库
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
|
5月前
|
关系型数据库 MySQL 数据库
【赵渝强老师】数据库不适合Docker容器化部署的原因
本文介绍了在Docker中部署MySQL数据库并实现数据持久化的方法,同时分析了数据库不适合容器化的原因。通过具体步骤演示如何拉取镜像、创建持久化目录及启动容器,确保数据安全存储。然而,由于数据安全性、硬件资源争用、网络带宽限制及额外隔离层等问题,数据库服务并不完全适合Docker容器化部署。文中还提到数据库一旦部署通常无需频繁升级,与Docker易于重构和重新部署的特点不符。
315 18
【赵渝强老师】数据库不适合Docker容器化部署的原因
|
7月前
|
SQL 关系型数据库 网络安全
Navicat Premium 17 最新版下载与配置:5分钟完成企业级数据库工具部署
Navicat Premium 17 是一款支持多种主流数据库(如 MySQL、Oracle、PostgreSQL 等)的多数据库管理工具,提供可视化数据建模、SQL 编辑和数据同步等功能。试用版提供 14 天全功能体验,商业版支持跨平台使用。安装环境要求 Windows 10/11 或 macOS 12.0+,最低配置为 4GB 内存。下载并解压安装包后,按步骤启动安装程序、接受许可协议、自定义安装路径并完成安装。首次运行时需激活许可证并配置数据库连接。常见问题包括无法写入注册表、试用期续费及连接数据库权限问题。高级功能涵盖 SSH 通道加速、自动化任务调度和性能调优建议。
1808 19
|
8月前
|
中间件 关系型数据库 数据库
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
211 19
|
8月前
|
数据库
【YashanDB 知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
**数据库主备切换简介** 在数据库正常或异常情况下,实现主备切换至关重要。若配置不当,主节点故障将影响业务使用,尤其在23.2版本中。原因包括资源紧张或主节点异常。解决方法涵盖手动和自动切换: 1. **一主一备部署**: - **手动切换**:支持Switchover(同步正常时)和Failover(主库损坏时)。 - **自动切换**:启用yasom仲裁选主开关。 2. **一主两备部署**: - 默认最大保护模式,自动切换开启。 需检查并配置自动切换以确保高可用性。经验总结:一主一备默认关闭自动切换,需手动开启;一主两备默认开启。
|
8月前
|
SQL 关系型数据库 数据库
【YashanDB 知识库】OM 仲裁节点故障后手工切换方案和 yasom 仲裁重新部署后重新纳管数据库集群方案
本文介绍了一主一备数据库集群的部署步骤。首先在OM节点上传并解压软件包至指定路径,随后通过调整安装参数、执行安装和集群部署完成数据库设置。接着,在主备节点分别配置环境变量,并查看数据库状态以确认安装成功。最后,针对OM仲裁故障提供了手动切换方案,包括构造故障场景、关闭自动切换开关及使用SQL命令进行主备切换,确保系统高可用性。

热门文章

最新文章