C#根据excel文件中的表头创建数据库表

C#根据excel文件中的表头创建数据库表

csharp 复制代码
private void button1_Click(object sender, EventArgs e)
        {
            
            string tableName = tableNameTextBox.Text;
            string connectionString = "";

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Excel Files|*.xlsx;*.xls";
                openFileDialog.Title = "Select an Excel file";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    tableName = tableNameTextBox.Text;
                    connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={filePath}; Extended Properties='Excel 12.0;HDR=YES;'";
                }
            }
            // SQL Server connection string
            string sqlConnectionString = "server=127.0.0.1;uid=sa;pwd=xyz@1230;database="+ datebasetext.Text;

            try
            {
                using (OleDbConnection excelConnection = new OleDbConnection(connectionString))
                {
                    excelConnection.Open();
                    DataTable schemaTable = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);

                    if (schemaTable.Rows.Count == 0)
                    {
                        MessageBox.Show("Excel文件列名为空!");
                        return;
                    }
                    DataView sortedView = schemaTable.DefaultView;
                    sortedView.Sort = "ORDINAL_POSITION ASC";
                    DataTable sortedSchemaTable = sortedView.ToTable();

                    // Create the SQL CREATE TABLE statement
                    string createTableQuery = $"CREATE TABLE {tableName} (";

                    List<string> existingColumnNames = new List<string>();

                    foreach (DataRow row in sortedSchemaTable.Rows)
                    {
                        string columnName = row["COLUMN_NAME"].ToString();
                        if (!existingColumnNames.Contains(columnName))
                        {
                            createTableQuery += $"[{columnName}] NVARCHAR(200), ";
                            existingColumnNames.Add(columnName);
                        }
                    }
                    createTableQuery = createTableQuery.TrimEnd(',', ' ');
                    createTableQuery += ")";
                    using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
                    {
                        sqlConnection.Open();
                        using (SqlCommand sqlCommand = new SqlCommand(createTableQuery, sqlConnection))
                        {
                            sqlCommand.ExecuteNonQuery();
                        }
                    }
                    MessageBox.Show("创建表成功!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("报错信息: " + ex.Message);
            }
        }
相关推荐
武子康41 分钟前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
无敌最俊朗@43 分钟前
02-SQLite 为了防止多人同时乱写,把整个数据库文件“当一本账本加锁”
jvm·数据库·oracle
小坏讲微服务1 小时前
MaxWell中基本使用原理 完整使用 (第一章)
大数据·数据库·hadoop·sqoop·1024程序员节·maxwell
赵渝强老师1 小时前
【赵渝强老师】MySQL集群解决方案
数据库·mysql
jason.zeng@15022072 小时前
my.cnf详解
运维·数据库·adb
百***62852 小时前
MySQL 常用 SQL 语句大全
数据库·sql·mysql
2501_915918412 小时前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
百***6972 小时前
MySQL数据库(SQL分类)
数据库·sql·mysql
只因在人海中多看了你一眼3 小时前
B.40.5.1-数据库基础与核心原理
数据库
2503_928411563 小时前
11.11 Express-generator和文件上传和身份认证
数据库·node.js·express