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