Search code examples
c#excelms-officeoledb

Missing rows reading generated Excel file


I am creating an Excel File using OleDB, it works perfectly, next, I add rows to the file using directly in Office Excel , after, when i try read the file using OleDB again, the rows i added manually does not appear, only the columns and rows i wrote when i created the file.

Here is the code:

//Creation of the file
        String connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Mode=ReadWrite; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=0'";
        OleDbConnection oledbconnection;

        oledbconnection = new OleDbConnection(connectionString);
        oledbconnection.Open();

        String query = "CREATE TABLE [Sheet1] (....) ";

        OleDbCommand cmd = new OleDbCommand(query, oledbconnection);
        cmd.ExecuteNonQuery();

        oledbconnection.Close();

it works perfectly, the file is created correctly, then, when im trying read the file after modify it, I'm doing this

        String connectionStringRead = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=1'";

        OleDbConnection oledbconnection;
        oledbconnection = new OleDbConnection(connectionStringRead);
        oledbconnection.Open();

        DataTable table = new DataTable();
        String sheetName;

        sheetName = oledbconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();

        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", oledbconnection);
        adapter.Fill(table);

        oledbconnection.Close();
        return table;

The table contains ONLY the original information, not the rows I added using MS Excel.

I don't understand this, can you help me please ?

thanks in advance


Solution

  • Never try like that before, but if

    sheetName = "Sheet1" '-------> or whatever
    

    It should be

    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "$]", oledbconnection);