inicio mail me! sindicaci;ón

How to come with C# deploy database

Now a lot of program, it is related to the database, when making installation accordingly, deploy database looks be like is a very complex issue. Actually with respect to my individual character, deploy database is very simple, roughly train of thought is as follows:

1. The DBMS that uses itself will produce the SQL script that the database founds;

2. Write a program to carry out SQL script namely next, achieve the goal that establishs a database thereby.

Use a for example below bright, what database server uses is SQL Server.

The SQL scenario that should have created in the database above all is foremost first, add following statements:

Use Master

GO

If Exists (select * From Sysdatabases Where Name=’mytest’ )

Drop Database Mytest

GO

Create Database Mytest

GO

Use Mytest

GO

Note: Among them ” Mytest ” it is the database name that should establish.

And the code of the program is as follows:

/ /—————————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;

}

}

}

Those who want an attention is in SQL script ” \r\n ” , be to cannot identify in SQLCommand, because this wants to replace,be blank space; Next ” GO ” also be to cannot identify in SQLCommand, but to make every statement is carried out, accordingly I am here, with ” ; ” will replace.

Note: The position of the position of the program and SQL script file falls for same catalog, if feel no-go, can go up in my foundation again outspread.

Bookmark:Digg Del.icio.us Reddit

Leave a Comment