Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# datatable Cannot update. Database or object is read-only

Tags:

c#

.net

datatable

private void Form1_Load(object sender, EventArgs e)
        {
            GetDataTable(@"C:\Documents and Settings\agordon\Desktop\ACTIVITYEX.log");
        }
        public System.Data.DataTable GetDataTable(string strFileName)
        {
            System.Data.OleDb.OleDbConnection conn = 
                new System.Data.OleDb.OleDbConnection
                    ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " 
                    + System.IO.Path.GetDirectoryName(strFileName) 
                    + ";Extended Properties = \"Text;HDR=YES;FMT=TabDelimited\"");
            conn.Open();
            string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
            System.Data.DataTable ds = new System.Data.DataTable("CSV File");
            adapter.Fill(ds);
            return ds;
        }

when THE SAME file is named .csv it does not give me an error; however when i rename it to .log it says Cannot update. Database or object is read-only. on adapter.Fill

is this a compiler error?

like image 900
Alex Gordon Avatar asked May 13 '26 01:05

Alex Gordon


1 Answers

An old post, but I just stepped into that problem. Please refer to this article : https://support.microsoft.com/en-us/kb/245407

Long story short : Jet Engine only accepts a few file extension when reading CSV File (Extended properties=TEXT,etc..).

Solutions :

  1. rename your file in a temp folder when reading it
  2. read it with another mecanism than JET (standard File.io.ReadAllLines)
  3. Add the extension in the windows registry as pointed out by antonio Bukala
like image 50
Simon Avatar answered May 14 '26 15:05

Simon