C# WinForm如何部署数据库

本文介绍了在C# WinForm应用中如何部署数据库,包括通过生成SQL脚本创建数据库,以及解决处理包含StoreProcedure的脚本问题。提供了一种在创建数据库失败时删除不完备数据库的方法。
摘要由CSDN通过智能技术生成

 现在好多程序,都是与数据库相关的,因此在做安装的时候,部署数据库看似是一件很复杂的事情。其实就我个人而言,部署数据库是很简单,大致的思路如下:
 

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值