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);
            }
        }
相关推荐
大嘴皮猴儿19 小时前
零基础入门:跨境电商产品图片多语言翻译的完整流程与跨马翻译实操
大数据·数据库·人工智能·自动翻译·教育电商
雷工笔记19 小时前
Navicat 备份与还原 PostgreSQL 数据库
数据库·postgresql·oracle
逸Y 仙X19 小时前
文章十四:ElasticSearch Reindex重建索引
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
冷血~多好19 小时前
mysql实现主从复制以及springboot实现读写分离
java·数据库·mysql·springboot
两年半的个人练习生^_^19 小时前
如何自己实现多数据源
数据库
AI周红伟19 小时前
周红伟:关于OpenClaw安全使用提醒
大数据·数据库·人工智能·安全·腾讯云·openclaw
Python大数据分析@19 小时前
Pandas相比Excel的优势是哪些?
excel·pandas
斯密码赛我是美女19 小时前
周报--2
android·数据库
marsh020619 小时前
23 openclaw防止SQL注入:参数化查询与ORM安全使用
数据库·sql·安全·ai·编程·技术
原来是猿19 小时前
为什么要配置环境变量?
linux·数据库·python