开发者

Strange error when filling a data adapter

开发者 https://www.devze.com 2022-12-24 13:12 出处:网络
I am receiving the following error in my code (c#, .Net 3.5, VS2008) when I try to connect to an Excel sheet and fill a OleDbDataAdapter with t开发者_Go百科he results of a query. First the error:

I am receiving the following error in my code (c#, .Net 3.5, VS2008) when I try to connect to an Excel sheet and fill a OleDbDataAdapter with t开发者_Go百科he results of a query. First the error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

And here is the code, which is honestly pretty simple:

var excelFileName = @"c:\Metadata_Tool.xlsm";
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=Excel 12.0;HDR=YES;", excelFileName);

var adapter = new OleDbDataAdapter("Select * FROM [Video Tagging XML]", connectionString);
var ds = new DataSet();

adapter.Fill(ds, "VTX");

DataTable data = ds.Tables["VTX"];

foreach (DataRow myRow in data.Rows)
{
    foreach (DataColumn myColumn in data.Columns)
    {
        Console.Write("\t{0}", myRow[myColumn]);
    }
    Console.WriteLine();
}

Console.ReadLine();

I get the error on the line adapter.Fill(ds,"VTX");. I did find a microsoft forum post saying to turn on JIT optimization in VS2008 from the Tools/Options/Debug/General menu, but this did not seem to help. Any help would be greatly appreciated thanks!


The error appears to be generated from within the ACE provider. My guess is that it's because your connection string is wrong.

Instead of:

var excelFileName = "c:/Metadata_Tool.xlsm";

You should have:

var excelFileName = @"c:\Metadata_Tool.xlsm";

Note the backslash instead of forward slash. You need the @ character at the front to prevent the backslash from being escaped. You also don't need the string.Format at all - you aren't formatting anything on this line.

0

精彩评论

暂无评论...
验证码 换一张
取 消