【问题标题】:Transferring Data From Excel to SQL Server Using F#使用 F# 将数据从 Excel 传输到 SQL Server
【发布时间】:2015-10-19 06:57:55
【问题描述】:

我目前正在使用 Visual Studio 中的 F# 在 Excel 中进行大量数据操作。我正在合并、删除和修改各种工作表/工作簿中的表格,并将最终结果放入新工作簿中。我很清楚有多种方法可以将数据从 Excel 工作表导出到 Microsoft SQl Server,但我想知道 F# 中是否有一些库支持此功能,以便我可以在 Visual Studio 中对其进行编码并移动从那里将数据导入到 SQL Server 中新创建的表中。我已经搜索了互联网,但没有遇到任何描述我正在寻找的东西。如果有人知道这是否可能,或者知道如何实现,我将不胜感激。 (我使用的是 SQL Server 2014)

【问题讨论】:

    标签: sql-server excel f#


    【解决方案1】:

    听起来Excel type provider 正是您要找的。​​p>

    要将数据插入数据库,您可以使用SqlDataConnection type provider,尽管可能有更好/更简单的方法。

    【讨论】:

      【解决方案2】:

      您可以将数据从 Excel 导入 SQL Server,只需从 Excel 复制和粘贴即可。但您需要从 Excel 到 SQL 的顺序完全相同。比如……

      如果您的 Excel 有 4 列:

      A B C D 1 1 1 1 2 2 2 2 3 3 3 3

      您的表格必须有 4 列才能复制和粘贴

      ColumnA ColumnB ColumnC ColumnD

      如果您有很多列并且无法排序...您可以将 .xls 保存在 .csv(逗号分隔)中,这样您可以更轻松地导入。

      你需要用这种方式读取.cvs

      var contents = File.ReadAllText(filename).Split('\n'); var csv = from line in contents select line.Split(',').ToArray();

      然后你需要写入你的数据库:

      using(SqlConnection openCon=new SqlConnection("your_connection_String"))
      {
        string saveStaff = "INSERT into tbl_staff (staffName,userID,idDepartment) VALUES (@staffName,@userID,@idDepartment)";
      
        using(SqlCommand querySaveStaff = new SqlCommand(saveStaff))
         {
           querySaveStaff.Connection=openCon;
           querySaveStaff.Parameters.Add("@staffName",SqlDbType.VarChar,30).Value=name;
           .....
           openCon.Open();
      
           openCon.Close();
         }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2013-07-06
        • 2019-12-24
        • 2016-12-04
        • 2016-01-13
        • 1970-01-01
        • 2010-10-29
        相关资源
        最近更新 更多