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;

转载于:https://www.cnblogs.com/Cpart/p/7073125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用以下步骤根据数据库自动生成文件夹: 1. 首先,确保已经连接到数据库,并且可以从中检索到必要的信息。可以使用C#中的各种数据库连接库,例如ADO.NET或Entity Framework。 2. 查询数据库获取需要生成文件夹的相关数据。例如,可以使用SELECT语句从表中检索需要生成文件夹的记录。 3. 使用C#中的System.IO命名空间下的Directory类创建目标文件夹。可以使用CreateDirectory方法指定文件夹的路径和名称。 4. 在循环中迭代数据库查询结果,为每个需要生成文件夹的记录创建文件夹。可以将文件夹名称作为查询结果的一部分来生成文件夹名称。 5. 对于每个记录,可以使用路径组合函数(如Path.Combine)创建完整的文件夹路径。此函数接受文件夹的父路径和文件夹名称,并生成完整的路径。 6. 通过在迭代中为每个记录创建一个文件夹,实现根据数据库自动生成文件夹。 以下是一个简单示例: ```csharp using System; using System.IO; using System.Data.SqlClient; class Program { static void Main() { string connectionString = "your_database_connection_string"; string query = "SELECT folderName FROM YourTable"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string folderName = reader["folderName"].ToString(); string folderPath = Path.Combine("your_destination_path", folderName); Directory.CreateDirectory(folderPath); } reader.Close(); } } } ``` 需要根据自己的实际需求和数据库结构进行适当的调整和修改。注意要替换示例代码中的数据库连接字符串、查询语句和目标路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值