【问题标题】:Transferring Excel data into SQL table with different columns将 Excel 数据传输到具有不同列的 SQL 表中
【发布时间】:2023-03-30 05:29:01
【问题描述】:

我希望用户使用 C# WinForms 应用程序一次将一个文件从 Excel 文件传输到 SQL。 Excel 文件由相似的列组成,因此可能有一些新列或缺少的列。行数据会有所不同。

例如:

Excel 文件 1: 名称、城市州
Excel 文件 2: 名称、城市、邮编
Excel 文件 3: 姓名、城市、县

在我现有的 SQL 表中,我有列:名称、城市、人口、学校

如何将具有相似列名的新 Excel 文件插入现有 SQL 数据库?

到目前为止,我的想法是将新的 Excel 文件数据复制到临时表中,然后将其插入现有的 SQL 表中。问题是,我不知道如何编写 C# 代码(或 SQL 查询)来插入比现有 SQL 表更多或更少列的新 Excel 数据。

【问题讨论】:

    标签: c# mysql excel


    【解决方案1】:

    为此,您需要 No-SQL,如果您需要输入尚未包含在表中的列,那么如果您使用 c# 到alter table,则 sql 不是一个好的选择,那么请注意后果。如果你确定前面所有可能的列名,那么在开始插入之前尝试改变你的表

    【讨论】:

      【解决方案2】:

      这应该不会太难。将数据从 Excel 导出到 SQL Server 中的临时表。 C# 代码可能看起来像这样。

      public void importdatafromexcel(string excelfilepath)
      {
          //declare variables - edit these based on your particular situation
          string ssqltable = "tdatamigrationtable";
          // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have
          different
          string myexceldataquery = "select student,rollno,course from [sheet1$]";
          try
          {
              //create our connection strings
              string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath +
              ";extended properties=" + "\"excel 8.0;hdr=yes;\"";
              string ssqlconnectionstring = "server=mydatabaseservername;user
              id=dbuserid;password=dbuserpassword;database=databasename;connection reset=false";
              //execute a query to erase any previous data from our destination table
              string sclearsql = "delete from " + ssqltable;
              sqlconnection sqlconn = new sqlconnection(ssqlconnectionstring);
              sqlcommand sqlcmd = new sqlcommand(sclearsql, sqlconn);
              sqlconn.open();
              sqlcmd.executenonquery();
              sqlconn.close();
              //series of commands to bulk copy data from the excel file into our sql table
              oledbconnection oledbconn = new oledbconnection(sexcelconnectionstring);
              oledbcommand oledbcmd = new oledbcommand(myexceldataquery, oledbconn);
              oledbconn.open();
              oledbdatareader dr = oledbcmd.executereader();
              sqlbulkcopy bulkcopy = new sqlbulkcopy(ssqlconnectionstring);
              bulkcopy.destinationtablename = ssqltable;
              while (dr.read())
              {
                  bulkcopy.writetoserver(dr);
              }
      
              oledbconn.close();
          }
          catch (exception ex)
          {
              //handle exception
          }
      }
      

      然后,在 SQL Server 中,将数据从临时表移动到最终生产表。您的 SQL 可能看起来像这样。

      insert into production
      select ...
      from staging
      where not exists 
      (
          select 1 from staging
          where staging.key = production.key
      )
      

      那是我的 .02。我认为这样你就能更好地控制整个过程。

      【讨论】:

        【解决方案3】:

        如果您正在阅读此问题,那么我对其他问题的回答可能对您有所帮助(只需创建一个类来处理每一行信息,处理 excel 文件并执行此处解释的批量插入):

        Bulk insert is not working properly in Azure SQL Server

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2017-06-01
          • 1970-01-01
          • 2013-07-06
          • 1970-01-01
          • 2019-12-24
          • 1970-01-01
          • 2012-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多