1. 程式人生 > >C# 自動生成數據庫

C# 自動生成數據庫

exception 生成 nsh 自動 數據 fda ext auth manage

1.窗體下的代碼

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是sql腳本文件路徑。
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代碼


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(50) not null,
`author` varchar(20) not null,
`isbn` char(20) not null,
`edition` varchar(10) not null,
`press` varchar(20) not null,
`publicData` datetime not null,
`catagory` varchar(50) not null,
`price` decimal not null,
`description` varchar(500) not null,
`pic` varchar(100) 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# 自動生成數據庫