Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

SQL upload XLS file to a SQL table

$
0
0

I am trying to upload a xls file to an empty table. The SQL server is on a network PC - a have full access to it and an ODBC connection that works. The excel files is located on my PC (i stored it on the server as well - still not working). If i do it manually with the import and export wizard from my PC it works. What i am strugling with is this:

  1. $oDb = ObjCreate("ADODB.Connection")
  2. ;~ $oDB.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\111.xls;Extended Properties=Excel 8.0")
  3. $oDB.Open ("sql server")
  4. ;~ $query = "SELECT * INTO [ODBC;Driver={SQL Server};Server=testing;Database=1111;].CSAT FROM [Survey$]"
  5. ;~ $query = "SELECT * INTO dbo.CSAT FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\111.xls',[Survey$])"
  6. $query = "SELECT * INTO dbo.CSAT FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\111.xls', 'SELECT * FROM [Customers$]')"
  7. $oSQLRS = $oDB.Execute ($query)
  8. $oDb = 0

I am trying to convert this:

Visual Basic         
  1. The following Visual Basic 6.0 code sample requires that you add a project reference to ADO. This code sample demonstrates how to import Excel data to SQL Server over an ADO connection by using the Jet 4.0 Provider.
  2.  
  3.     Dim cn As ADODB.Connection
  4.     Dim strSQL As String
  5.     Dim lngRecsAff As Long
  6.     Set cn = New ADODB.Connection
  7.     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  8.         "Data Source=C:\test\xltestt.xls;" & _
  9.         "Extended Properties=Excel 8.0"
  10.    
  11.     'Import by using Jet Provider.
  12.    strSQL = "SELECT * INTO [odbc;Driver={SQL Server};" & _
  13.         "Server=<server>;Database=<database>;" & _
  14.         "UID=<user>;PWD=<password>].XLImport9 " & _
  15.         "FROM [Customers$]"
  16.     Debug.Print strSQL
  17.     cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
  18.     Debug.Print "Records affected: " & lngRecsAff
  19.        
  20.     cn.Close
  21.     Set cn = Nothing
  22.                

Or this:

Visual Basic         
  1. Use ADO and SQLOLEDB
  2. When you are connected to SQL Server in an ADO application by using Microsoft OLE DB for SQL Server (SQLOLEDB), you can use the same "distributed query" syntax from the Using Distributed Queries section to import Excel data into SQL Server.
  3.  
  4. The following Visual Basic 6.0 code sample requires that you add a project reference to ActiveX Data Objects (ADO). This code sample also demonstrates how to use OPENDATASOURCE and OPENROWSET over an SQLOLEDB connection.
  5.  
  6.     Dim cn As ADODB.Connection
  7.     Dim strSQL As String
  8.     Dim lngRecsAff As Long
  9.     Set cn = New ADODB.Connection
  10.     cn.Open "Provider=SQLOLEDB;Data Source=<server>;" & _
  11.         "Initial Catalog=<database>;User ID=<user>;Password=<password>"
  12.  
  13.     'Import by using OPENDATASOURCE.
  14.    strSQL = "SELECT * INTO XLImport6 FROM " & _
  15.         "OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _
  16.         "'Data Source=C:\test\xltest.xls;" & _
  17.         "Extended Properties=Excel 8.0')...[Customers$]"
  18.     Debug.Print strSQL
  19.     cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
  20.     Debug.Print "Records affected: " & lngRecsAff
  21.  
  22.     'Import by using OPENROWSET and object name.
  23.    strSQL = "SELECT * INTO XLImport7 FROM " & _
  24.         "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
  25.         "'Excel 8.0;Database=C:\test\xltest.xls', " & _
  26.         "[Customers$])"
  27.     Debug.Print strSQL
  28.     cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
  29.     Debug.Print "Records affected: " & lngRecsAff
  30.  
  31.     'Import by using OPENROWSET and SELECT query.
  32.    strSQL = "SELECT * INTO XLImport8 FROM " & _
  33.         "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
  34.         "'Excel 8.0;Database=C:\test\xltest.xls', " & _
  35.         "'SELECT * FROM [Customers$]')"
  36.     Debug.Print strSQL
  37.     cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
  38.     Debug.Print "Records affected: " & lngRecsAff
  39.  
  40.     cn.Close
  41.     Set cn = Nothing
  42.                

I failed. Has anyone ever done something like this?


Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>