RE: Excel to Sql

From: Ken M. (anonymous_at_discussions.microsoft.com)
Date: 03/01/04

  • Next message: Carol Browning: "How to connect to sql server 2000 using ADO"
    Date: Mon, 1 Mar 2004 11:46:12 -0800
    
    

    I think I found it:

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q321686#4e

    Use ADO and SQLOLEDB
    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.

    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.

        Dim cn As ADODB.Connection
        Dim strSQL As String
        Dim lngRecsAff As Long
        Set cn = New ADODB.Connection
        cn.Open "Provider=SQLOLEDB;Data Source=<server>;" & _
            "Initial Catalog=<database>;User ID=<user>;Password=<password>"

        'Import by using OPENDATASOURCE.
        strSQL = "SELECT * INTO XLImport6 FROM " & _
            "OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _
            "'Data Source=C:\test\xltest.xls;" & _
            "Extended Properties=Excel 8.0')...[Customers$]"
        Debug.Print strSQL
        cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
        Debug.Print "Records affected: " & lngRecsAff

        'Import by using OPENROWSET and object name.
        strSQL = "SELECT * INTO XLImport7 FROM " & _
            "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
            "'Excel 8.0;Database=C:\test\xltest.xls', " & _
            "[Customers$])"
        Debug.Print strSQL
        cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
        Debug.Print "Records affected: " & lngRecsAff

        'Import by using OPENROWSET and SELECT query.
        strSQL = "SELECT * INTO XLImport8 FROM " & _
            "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _
            "'Excel 8.0;Database=C:\test\xltest.xls', " & _
            "'SELECT * FROM [Customers$]')"
        Debug.Print strSQL
        cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
        Debug.Print "Records affected: " & lngRecsAff

        cn.Close
        Set cn = Nothing
                                    


  • Next message: Carol Browning: "How to connect to sql server 2000 using ADO"

    Relevant Pages

    • Re: Unspecified error while passingTEXT fields containing Hebrew t
      ... I am using ADO to communicate with SQL Server and the PRovider is ... > I assume that the engine is SQL Server, and the provider is SQLOLEDB? ...
      (microsoft.public.data.oledb)
    • RE: SQL for SP
      ... But you can access this value with a recordset object (ADO). ... strSQL = strSQL & "SELECT SCOPE_IDENTITYas newIdent" ... >> much more experience with VB than with SQL Server and have more control on ...
      (microsoft.public.sqlserver.programming)
    • RE: SQL for SP
      ... But you can access this value with a recordset object (ADO). ... strSQL = strSQL & "SELECT SCOPE_IDENTITYas newIdent" ... >> much more experience with VB than with SQL Server and have more control on ...
      (microsoft.public.vb.database)
    • RE: SQL for SP
      ... But you can access this value with a recordset object (ADO). ... strSQL = strSQL & "SELECT SCOPE_IDENTITYas newIdent" ... >> much more experience with VB than with SQL Server and have more control on ...
      (microsoft.public.vb.database.ado)
    • RE: SQL for SP
      ... But you can access this value with a recordset object (ADO). ... strSQL = strSQL & "SELECT SCOPE_IDENTITYas newIdent" ... >> much more experience with VB than with SQL Server and have more control on ...
      (microsoft.public.vb.general.discussion)