【问题标题】:Getting "[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Microsoft.'获取“[Microsoft][ODBC SQL Server Driver][SQL Server]'Microsoft'附近的语法不正确。'
【发布时间】:2010-10-24 18:45:11
【问题描述】:

我收到一条错误消息,“[Microsoft][ODBC SQL Server Driver][SQL Server]'Microsoft'附近的语法不正确。

代码如下:

Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String




Public Sub loadData()
 'This was set up using Microsoft  ActiveX Data Components version 6.0.



 'Create ADODB connection object, open connection and 
 '       construct the connection string object which is the DSN name.
 Set conn = New ADODB.Connection
 conn.ConnectionString = "sql_server"

conn.Open
'conn.Execute (strSQL)

On Error GoTo ErrorHandler

'Open Excel and run query to export data to SQL Server.
strSQL = "SELECT * INTO SalesOrders " & _
         "FROM OPENDATASOURCE(Microsoft.ACE.OLEDB.12.0;" & _
         "Data Source=C:\Workbook.xlsx;" & _
         "Extended Properties=Excel 12.0; [Sales Orders])"

conn.Execute (strSQL)

'Error handling.
ErrorExit:
 'Reclaim memory from the cntection objects
 Set rst = Nothing
 Set conn = Nothing

Exit Sub

ErrorHandler:
   MsgBox Err.Description, vbCritical
   Resume ErrorExit


'clean up and reclaim memory resources.
conn.Close
If CBool(cnt.State And adStateOpen) Then
Set rst = Nothing
Set conn = Nothing

End If

End Sub

【问题讨论】:

    标签: sql-server excel vba


    【解决方案1】:

    传递给 OPENDATASOURCE 的提供程序参数是一个字符串,因此必须加引号。 您还在 OPENDATASOURCE 调用中处理工作表,这是不正确的;

         strSQL = "SELECT * INTO SalesOrders " & _
           "FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'," & _
           "'Data Source=C:\Workbook.xlsx;" & _
           "Extended Properties=Excel 12.0')...[Sales Orders]"
    

    【讨论】:

    • 好的。我明白你的意思了。我已经进行了更改,但现在我得到了 [Microsoft][ODBC SQL Server Driver][SQL Server]OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" 返回消息 "Unspecified error" .
    • 确保您运行该查询的帐户有权访问该文件(可能需要读取和写入)并确保该文件未在其他任何地方打开。 (还要记住该路径中的 C:\ 是服务器上的 C 盘!)
    • 感谢您的帮助,亚历克斯。你也是拉吉!
    【解决方案2】:

    试试下面的语法

    xls

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[FilePath];Extended Properties=”Excel 8.0;HDR=YES;IMEX=1”
    

    xlsx

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[FilePath];Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1
    

    HDR=Yes 指定数据的第一行包含列名而不是数据,因此请进行相应设置(是,否)

    IMEX=1 指定驱动程序应始终将“混合”数据列作为文本读取

    参考:http://weblogs.asp.net/rajbk/archive/2009/05/02/uploading-an-excel-file-to-sql-through-an-asp-net-webform.aspx

    【讨论】:

    • 我输入了“Provider=Microsoft...”,但现在不是“Microsoft 附近的语法不正确”,而是“Provider 附近的语法不正确”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多