views:

163

answers:

1

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 to read Excel file created by Excel 2007 (but saved as Excel 2003 xls format). Here is my code, when executing code adapter.Fill(cities), there is exception -- "OldDbException Could not find installable ISAM". Any ideas what is wrong?

static void Main(string[] args)
{
    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\data.xls;ExtendedProperties=""Excel 8.0;HDR=YES;""";

    DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

    DbDataAdapter adapter = factory.CreateDataAdapter();

    DbCommand selectCommand = factory.CreateCommand();
    selectCommand.CommandText = "SELECT ID FROM [Sheet1$]";

    DbConnection connection = factory.CreateConnection();
    connection.ConnectionString = connectionString;

    selectCommand.Connection = connection;

    adapter.SelectCommand = selectCommand;

    DataSet cities = new DataSet();

    adapter.Fill(cities); // OldDbException Could not find installable ISAM

    return;
}

thanks in advance, George

+2  A: 

I replicated your issue, replace your code with the following and it should work

static void Main ( string [] args )
    {
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\Data.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
        DbProviderFactory factory = DbProviderFactories.GetFactory ( "System.Data.OleDb" );

        using ( DbConnection connection = factory.CreateConnection ( ) )
        {
            connection.ConnectionString = connectionString;

            using ( DbCommand command = connection.CreateCommand ( ) )
            {
                // Cities$ comes from the name of the worksheet
                command.CommandText = "SELECT ID  FROM [Sheet1$]";

                connection.Open ( );

                using ( DbDataReader dr = command.ExecuteReader ( ) )
                {
                    DataSet cities = new DataSet ( );
                    cities.Load ( dr, LoadOption.OverwriteChanges, new [] { "Sheet1" } );
                }
            }
        }

        return;
    }
Simon Wilson
"Do you have ADO installed on the machine running this code?" -- how to check? I installed full VSTS 2008, just assume everything is there... :-)
George2
See my edit, as far as I am aware VS does not install MDAC components
Simon Wilson
Your code works, cool!
George2