1
using (OleDbConnection connection = new 
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\""))
{
    using (OleDbCommand command = new OleDbCommand(sql, connection))
    {
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {
            dataTable = new DataTable();
            adapter.Fill(dataTable);
        }
    }
}

The above is the code I am using and getting the below error:

Cannot update. Database or object is read-only.

Have someone faced the same issue?

Hadi
  • 36,233
  • 13
  • 65
  • 124

2 Answers2

0

your connection string contains "Readonly=1;". try changing "Readonly=0;". And try to remove "imex=1". So your code should be someting like that:

    using (OleDbConnection connection = new 
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + ";Readonly=0;Extended Properties=Excel 8.0;\""))
{
    using (OleDbCommand command = new OleDbCommand(sql, connection))
        {
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                {
                    dataTable = new DataTable();
                    adapter.Fill(dataTable);
                 }
         }
}
MZG
  • 51
  • 4
0

Try using the following syntax (remove Text from Extended properties since it is used to import csv files) :

using (OleDbConnection connection = new 
OleDbConnection(String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"HDR={1};IMEX=1;Readonly=1;Extended Properties=Excel 8.0;\"",pathOnly ,header )))
{
    using (OleDbCommand command = new OleDbCommand(sql, connection))
    {
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {
            dataTable = new DataTable();
            adapter.Fill(dataTable);
        }
    }
}

If you have office 2007 or higher provider installed try using Microsoft.ACE.OLEDB.12.0 provider since the it also support reading old excel formats.

If you are trying to import text files (csv) then it is better to use text parsing libraries such as :

Hadi
  • 36,233
  • 13
  • 65
  • 124