Results 1 to 3 of 3

Thread: excel .net 2.0

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    excel .net 2.0

    Hi,
    Trying to read the data from an excel spreadsheet in C# 2005, but there is an error as follows:
    The Microsoft Jet database engine could not find the object 'Cities$'. Make sure the object exists and that you spell its name and the path name correctly.

    The excel speadsheet is placed in the same place as Program.cs and the sheet name is indeed cities.

    This is the code:

    Private Sub btnExcel_Click(sender As Object, e As EventArgs)
    Dim connectionString As String = ""
    Dim factory As DbProviderFactory = DbProviderFactories.GetFactory("System.Data.OleDb")

    Dim connection As DbConnection = factory.CreateConnection()
    Try
    connection.ConnectionString = connectionString

    Dim command As DbCommand = connection.CreateCommand()
    Try
    ' Cities$ comes from the name of the worksheet
    command.CommandText = "SELECT ID,City,State FROM [Cities$]"

    connection.Open()

    Dim dr As DbDataReader = command.ExecuteReader()
    Try 'fails here...
    While dr.Read()
    Console.WriteLine(dr("ID").ToString())
    End While
    Finally
    dr.Dispose()
    End Try
    Finally
    command.Dispose()
    End Try
    Finally
    connection.Dispose()
    End Try
    End Sub 'btnExcel_Click

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: excel .net 2.0

    You need to supply the connection string to your connection.
    VB Code:
    1. 'You had
    2. Dim connectionString As String = ""
    3. 'Then later you set
    4. connection.ConnectionString = connectionString
    5. 'So the connection string of your connection is empty.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: excel .net 2.0

    Moved to the C# forum.


    You should use this connectionstring.


    VB Code:
    1. connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width