如何用c#来部署数据库 _c#应用
2008-02-23 05:42:00来源:互联网 阅读 ()
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;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash