码迷,mamicode.com
首页 > 数据库 > 详细

C# 自动生成数据库

时间:2017-06-24 13:54:39      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:.sh   窗体   .text   dir   dia   label   tin   tostring   drop   

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# 自动生成数据库

标签:.sh   窗体   .text   dir   dia   label   tin   tostring   drop   

原文地址:http://www.cnblogs.com/Cpart/p/7073125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!