SQL Server批量插入文件不存在

时间:2018-06-05 10:49:12

标签: sql-server

create table StudentInfo
(
Name nvarchar(100) ,
AdminNo nvarchar(10) ,
Gender char(1) ,
BirthDate datetime ,
BGRStatus nvarchar(10) ,
)


bulk insert StudentInfo from 'C:\Users\Tracy\Desktop\StudentInfo\StudentInfo.txt'

执行后,给出的消息是' Msg 4860,Level 16,State 1,Line 34 无法批量加载。文件" C:\ Users \ Tracy \ Desktop \ StudentInfo \ StudentInfo.txt"不存在。'

但文件显然在那里

1 个答案:

答案 0 :(得分:0)

几个月前,我遇到了同样的问题。如Lukasz所述,必须从SQL Server实例而不是本地计算机可以访问该文件。

本质上...

The UNC path must be visible to the remote SQL Server instance.

    The account that SQL Server runs as must be a domain account with network privileges and rights to the remote file.

我想出了以下方法将数据加载到SQL Server。

#1)  Convert your TXT file to XLSB (or XLSM).

#2)  Run the VBA script below.



Sub TestExportToSQLServer()
    Dim Cn As ADODB.Connection
    Dim ServerName As String
    Dim DatabaseName As String
    Dim TableName As String
    Dim UserID As String
    Dim Password As String
    Dim rs As ADODB.Recordset
    Dim RowCounter As Long
    Dim NoOfFields As Integer
    Dim StartRow As Long
    Dim EndRow As Long
    Dim ColCounter As Integer

    Set rs = New ADODB.Recordset

    ServerName = "server_name" ' Enter your server name here
    DatabaseName = "db_name" ' Enter your  database name here
    TableName = "customer_master" ' Enter your Table name here
    UserID = "" ' Enter your user ID here
    ' (Leave ID and Password blank if using windows Authentification")
    Password = "" ' Enter your password here
    NoOfFields = 331 ' Enter number of fields to update (eg. columns in your worksheet)
    StartRow = 2 ' Enter row in sheet to start reading  records
    EndRow = 106695 ' Enter row of last record in sheet

     '  CHANGES
    Dim shtSheetToWork As Worksheet
    Set shtSheetToWork = ActiveWorkbook.Worksheets("customer_master")
     '********

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
    ";Uid=" & UserID & ";Pwd=" & Password & ";"

    rs.Open TableName, Cn, adOpenKeyset, adLockOptimistic

     'EndRow = shtSheetToWork.Cells(Rows.Count, 1).End(xlUp).Row
    For RowCounter = StartRow To EndRow
        rs.AddNew
        For ColCounter = 1 To NoOfFields
        'On Error Resume Next
            rs(ColCounter - 1) = shtSheetToWork.Cells(RowCounter, ColCounter)
        Next ColCounter
        Debug.Print RowCounter
    Next RowCounter
    rs.UpdateBatch

     ' Tidy up
    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing

End Sub

设置对Microsoft ActiveX Data Objects 2.8 Library

的引用