首页 > 代码库 > C# 自动生成数据库
C# 自动生成数据库
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# 自动生成数据库