7

我有一个 Excel 电子表格,其中包含我需要放入 SQL Server 数据库的所有数据。我是 ASP.NET 的新手,以前从未从 Excel 导出到 SQL Server。

我的 Excel 电子表格看起来像这样

贸易标题 -> ArtID -> 商家名称 -> AdStyleCode -> 地址 -> 郊区

在 SQL Server 中,我创建了一个名为“Listings”的表,格式为

intListingID -> intCategoryID -> BusinessName - ArtID -> intAdCode ->Address -> Suburb

从 Excel 导出数据然后将其导入 SQLServer 2005 的最佳方法是什么。

谢谢...

4

2 回答 2

4

您可以使用 SSIS 轻松完成此操作,您可以参考这两个链接了解完整的详细信息。

  1. 链接 1
  2. 链接 2

[编辑]

如果您有 Express,那么您可以尝试以下命令来设置链接服务器并获取数据

EXEC sp_addlinkedserver ExcelData,'Jet 4.0','Microsoft.Jet.OLEDB.4.0','C:\MyData.xls', NULL, 'Excel 5.0;'
GO

然后您可以将数据选择到您的表中

INSERT INTO Listings ...
SELECT column1 AS intListingID, <put all columns here> FROM ExcelData...Data
GO

有关其他选项,请查看此链接

于 2009-04-06T08:57:19.977 回答
0

我尝试通过 VBA 将数据从 Excel 导出到 SQL 服务器,如下所示:

Const myDB As String = "tenant"
Const myServer As String = "MPAADM"
Const myDB As String = "new"
Const myServer As String = "arjun"

Sub ExportTableToSQL()

    Dim cn As ADODB.Connection
    Dim cnSQL As ADODB.Connection
    Dim strSQL As String
    Dim lngRecsAff As Long
    Dim sqlTable As String
    Dim acell As Range
    Dim t1 As Date
    Dim t2 As Date
    Dim column As String

    On Error Resume Next
    path = Sheets("Sheet2").Range("B1").Value

    Kill path & \z_temp\aa.xls"
    Kill path & "\z_temp\aa.xls"

    On Error GoTo 0

    On Error GoTo 10
    column = Cells(1, 1).Value

        sqlTable = InputBox("Insert SQL table name")
    Application.Wait (Now + TimeValue("0:00:2"))
    t1 = Now
    Debug.Print t1
    If sqlTable = "" Then Exit Sub
    Application.ScreenUpdating = False
    Set acell = ActiveCell
    If IsEmpty(ActiveCell) Then
        MsgBox "Select a cell inside a table you want to export to SQL"
        Exit Sub
    End If
    ActiveCell.CurrentRegion.Select
    Selection.Copy
    Call NewWorkbook
    Cells(1, 1).Select
    ActiveSheet.Paste

    Set cn = New ADODB.Connection
    Set cnSQL = New ADODB.Connection
    With cnSQL
        .ConnectionString = "provider=sqloledb;Data Source=" & myServer & ";Initial Catalog=" & myDB & ";Trusted_Connection=Yes"
        '      .ConnectionString = "provider=sqloledb;Data Source=" & myServer & ";Initial Catalog=" & myDB & ";Uid=jayantuser;Pwd=Input@123"
        .Open
    End With


    ActiveWorkbook.SaveAs path & "\z_temp\aa.xls"

    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & path & "\z_temp\aa.xls;" & _
    "Extended Properties=Excel 12.0"


    strSQL = "drop table " & sqlTable
    Debug.Print strSQL
    On Error Resume Next
    cnSQL.Execute strSQL, lngRecsAff, adExecuteNoRecords
    Err.Clear
    On Error GoTo 10

    strSQL = "SELECT * INTO [odbc;Driver={SQL Server};" & _
        "Server=" & myServer & ";Database=" & myDB & _
        ";trusted_connection=yes]." & sqlTable & _
        " FROM [sheet1$]"

Debug.Print strSQL    
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords    
    cn.Close
    Set cn = Nothing
    ActiveWorkbook.Close False
    On Error Resume Next
    Kill path & "\z_temp\aa.xlx"
    On Error GoTo 0
    t2 = Now
    Debug.Print t2
    MsgBox sqlTable & " table was successfully imported into SQL Server" & vbNewLine & "Transfered record number: " & lngRecsAff _
    & vbNewLine & "Time:" & Int((t2 - t1) * 86400) & "s"
    If MsgBox("Convert data type to bigint?", vbYesNo) = vbYes Then
        strSQL = "ALTER TABLE " & sqlTable & " ALTER COLUMN " & column & " bigint"
        cnSQL.Execute strSQL, lngRecsAff, adExecuteNoRecords
    End If 
    Application.ScreenUpdating = True
    acell.Select
    Exit Sub
10: MsgBox Err.Description
End Sub                                                                       `Sub NewWorkbook()
    Application.DefaultSaveFormat = xlOpenXMLWorkbook
    Workbooks.Add
End Sub`                                                                     
   `Sub Quit()
    Application.Quit
End Sub`                                                                                             
于 2018-03-24T18:06:25.080 回答