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;

时间: 2024-08-27 16:13:38

C# 自动生成数据库的相关文章

自动生成数据库字典(sql2008) 转自 飘渺の云海

每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 截取一部分图片: 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Carbe> -- Create date: <2014-09-19> -- Description: &

MVC自动生成数据库【Code-FIrst方式】

一般我们写好实体之后,配置好数据上下文对象,还有在配置文件中改好连接字符串之后. 还不能生成数据库,自动生成数据库,有两步关键步骤: 1.   Enable Migrations 2. Update-database 执行玩第二步之后,就自动生成了数据库. ------------------------------------实例---------------------------------------------------------------- //实体: using System

自动生成数据库字典(sql2008)

每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Carbe> -- Create date: <2014-09-19> -- Description: <生成数据库字

Entityframework Codefirst模式自动生成数据库方式

EF在codefirst模式下能自动生成数据库,那它是如何生成的呢,默认情况下如何生成的呢?先来看看用来生成数据库的DbContext类吧 1 protected DbContext(); 2 protected DbContext(DbCompiledModel model); 3 public DbContext(string nameOrConnectionString); 4 public DbContext(DbConnection existingConnection, bool c

完善实体类,由EF自动生成数据库过程中的一些问题

①.配置两表间的关系时WillCascadeOnDelete(false)取消级联删除 public AdminLogConfig() { this.ToTable("T_AdminLogs"); this.Property(a => a.Msg).IsRequired(); this.HasRequired(a => a.AdminUser).WithMany().HasForeignKey(a => a.AdminUserId).WillCascadeOnDele

hibernate 自动生成数据库表

只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update</property> update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表:如果表里缺少列,则自动添加列. 还有其他的参数: create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失.

Hibernate自动生成数据库表

在hibernate.cfg.xml中添加: 引用 <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> value的值可选项如下: 引用 validate  加载hibernate时,验证创建数据库表结构 create  每次加载hibernate,重新创建数据库表结构. create-drop  加载hibern

hibernate+mysql 自动生成数据库问题

Hibernate Entity类 表名注解大写时,在windows下mysql自动生成的表都为小写(不区分大小写),在linux下mysql自动生成区分大小写.导致数据库问题. 原因(window下mysql不区分大小写,hibernate生成时全部生成小写,linux下生成时,按照注解大小写生成) 1 package com.pera.report.designer.data; 2 3 import java.sql.Clob; 4 5 import javax.persistence.Ba

springBoot自动生成数据库表

@Entity @Table(name="t_book") //表名  name 不一定有用 public class Book { @Id    //表的id @GeneratedValue  //自增属性 private Integer id; @Column(length=100) //String 自动对应字符串 private String name; @Column(length=50) private String author; 会根据注释生成对应的表 但是我在生成标的

ASP.NET MVC5利用EF,反向自动生成数据库

1.在Model类里面,写好相应的属性. 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.Entity; 6 7 namespace MvcMovie.Models 8 { 9 public class Movie 10 { 11 public int ID { get; set; } 12 public string