C # Automatic database generation

Source: Internet
Author: User

1. Code under the form

private void Button1_Click (object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog ();
Of. Filter = "*.sql|*.*";
Of. InitialDirectory = Application.startuppath; ;//application.startuppath;
Of. Title = "Select the SQL File";
if (of. ShowDialog () = = DialogResult.OK)
{
Label3. Text = of. FileName;

Button2. Enabled = true;
}
}

private void Button2_Click (object sender, EventArgs e)
{
var connstr = configurationmanager.appsettings["ConnectionString"];

Mysqlconnection conn = new mysqlconnection (CONNSTR);
Try
{
Label2. Text + = "Connecting to mysql...\r\n";
Conn. Open ();
System.IO.FileInfo file = new System.IO.FileInfo (label3. Text); FileName is the path to the SQL script file.
String sql = file. OpenText (). ReadToEnd ();
Mysqlscript script = new Mysqlscript (conn);
Script. Query = SQL;
int count = script. Execute ();
Label2. Text + = "Executed" + Count + "statement (s) \ r \ n";
Label2. Text + = "Delimiter:" + script. Delimiter + "\ r \ n";
Textbox_log. Text + = "Query:" + script. Query + "\ r \ n";
}
catch (Exception ex)
{
Label2. Text + = ex. ToString ();
}
Conn. Close ();
Label2. Text + = "Execute successfully.";
}

private void Form1_Load (object sender, EventArgs e)
{
Button2. Enabled = false;
}

2.sql Code


DROP database IF EXISTS ' DBNAME11 ';

Create database ' DBName11 ';

Use ' DBName11 ';

DROP TABLE IF EXISTS ' T_book ';
CREATE TABLE ' T_book '
(
' bookId ' int unsigned primary key not NULL auto_increment,
' Name ' varchar (NOT NULL),
' Author ' varchar (NOT NULL),
' ISBN ' char (not NULL),
' Edition ' varchar () NOT NULL,
' Press ' varchar (NOT NULL),
' Publicdata ' datetime NOT NULL,
' catagory ' varchar (NOT NULL),
' Price ' decimal is not NULL,
' Description ' varchar (+) NOT NULL,
' pic ' varchar (+) NOT NULL,
' Sold ' int unsigned NOT NULL,
' Sum ' int unsigned NOT NULL,
' Upshelfdate ' datetime NOT NULL,
' Downshelfdate ' datetime NOT NULL
) Engine=innodb DEFAULT charset=latin1;

C # Automatic database generation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.