transfer data from Excel to SQL Server

后端 未结 2 1859
别那么骄傲
别那么骄傲 2021-02-09 08:33

I have an Excel Spreadsheet that contains all my data that I need to put into an SQL Server database. I am fairly new o ASP.NET and have never had to export from Excel to SQL Se

2条回答
  •  野性不改
    2021-02-09 09:16

    I tried exporting data from Excel to SQL server through VBA like this:

    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`                                                                                             
    

提交回复
热议问题