将文件从 Excel 2010 导入到 Visual Studio SQL Server

发布于 2024-10-18 14:50:24 字数 173 浏览 3 评论 0原文

因此,我在 Excel 中有一个巨大的文件,我想将其用于 Visual Studio WPF 中的用户界面。我将其余数据库存储在 Visual Studio 附带的 SQL Server 中。有什么方法可以将这些数据从 Excel 导入到我的 Visual Studio Sql Server 中吗?

感谢您的帮助。

So I have a huge huge file in Excel that I want to use for my User Interface in Visual Studio WPF. I have my rest of the database stored in SQL Server that came with Visual Studio. Is there any way I could Import this data from Excel into my Visual Studio Sql Server?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-10-25 14:50:24

我个人会使用 Microsoft.Office.Interop.Excel 命名空间
并编写一些类似这样的代码,将文件保存为 CSV,然后使用 BULK COPY 命令将其转储到 SQL 中,或者在 Microsoft c#.net 中,您可以使用

SqlBulkCopy 对象来执行此操作。

因此,您将逐行读取 CSV 并将其写入数据表。然后使用以下命令写入SQL。

VB.NET 中的bulkCopy 对象示例(抱歉,我在 vb.net 中使用它,而不是在 C# 中使用它)

 Public Function InsertDataToDatabase(ByVal _strDestinationTableName As String, ByRef _dtData As DataTable, ByRef _sqlConnection As SqlConnection) As Boolean
        Try

            RaiseEvent BulkCopyStartEvent(Me, _dtData.Rows.Count())

            OpenConnection()

            sBulkCopy = New SqlBulkCopy(DatabaseConnection)

            'Clear out all data in the TmpTable

            Dim sqlComm As New SqlCommand(String.Format("TRUNCATE TABLE {0}", _strDestinationTableName), DatabaseConnection)
            sqlComm.ExecuteNonQuery()

            With sBulkCopy

                sBulkCopy.DestinationTableName = _strDestinationTableName
                sBulkCopy.NotifyAfter = _dtData.Rows.Count / 100 ' Notify after every 1%
                sBulkCopy.WriteToServer(_dtData)
                sBulkCopy.Close()
            End With

            RaiseEvent BulkCopyCompleteEvent(Me, _dtData.Rows.Count(), arrExceptionStringList.Count())

            Return True

        Catch ex As BulkCopyUtilityErrorException
            RaiseEvent BulkCopyErrorEvent(Me, ex)
        Finally
            CloseConnection()
        End Try
    End Function

Personally I would use the Microsoft.Office.Interop.Excel Namespace
and write some code like this save the file as a CSV, and then dump it into SQL using either the BULK COPY command or in Microsoft c#.net you can use the

SqlBulkCopy object to do this.

So you would read the CSV line by line and write it out to a DataTable. then use the following to write to SQL.

Example of the bulkCopy Object in VB.NET (Sorry i am using it in vb.net not c# for something)

 Public Function InsertDataToDatabase(ByVal _strDestinationTableName As String, ByRef _dtData As DataTable, ByRef _sqlConnection As SqlConnection) As Boolean
        Try

            RaiseEvent BulkCopyStartEvent(Me, _dtData.Rows.Count())

            OpenConnection()

            sBulkCopy = New SqlBulkCopy(DatabaseConnection)

            'Clear out all data in the TmpTable

            Dim sqlComm As New SqlCommand(String.Format("TRUNCATE TABLE {0}", _strDestinationTableName), DatabaseConnection)
            sqlComm.ExecuteNonQuery()

            With sBulkCopy

                sBulkCopy.DestinationTableName = _strDestinationTableName
                sBulkCopy.NotifyAfter = _dtData.Rows.Count / 100 ' Notify after every 1%
                sBulkCopy.WriteToServer(_dtData)
                sBulkCopy.Close()
            End With

            RaiseEvent BulkCopyCompleteEvent(Me, _dtData.Rows.Count(), arrExceptionStringList.Count())

            Return True

        Catch ex As BulkCopyUtilityErrorException
            RaiseEvent BulkCopyErrorEvent(Me, ex)
        Finally
            CloseConnection()
        End Try
    End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文