Я использую ODBC Connection to Excel 2016 в c # Visual Studio 2017. Я загрузил ядро базы данных Microsoft Access, которое можно распространять в операционной системе Windows 10, в которой 64-разрядная версия Office 2016 уже загружена. Но мой exe-файл не всегда часто портит драйверы в odbc-соединении. Мне приходится каждый раз переустанавливать движок базы данных Microsoft Access. Пожалуйста, дайте мне знать решение этой проблемы.
public DataTable ConvertExcelToDataTable(string FileName, string sheetName)
{
DataTable dtResult = null;
int totalSheet = 0; //No of sheets on excel file
using (OleDbConnection objConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties='Excel 12.0;text;HDR=NO;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text';"))
{
objConn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt != null)
{
var tempDataTable = (from dataRow in dt.AsEnumerable()
where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
select dataRow).CopyToDataTable();
dt = tempDataTable;
totalSheet = dt.Rows.Count;
// sheetName = dt.Rows[0]["TABLE_NAME"].ToString();
}
cmd.Connection = objConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
// dtResult.Rows.Add(new System.Data.DataRows("NodeID"));
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds, "Data");
DataRow rowDel = ds.Tables["Data"].Rows[0];
int ctr = 0;
foreach (DataColumn dc in ds.Tables["Data"].Columns)
{
string val = rowDel[ctr].ToString();
if (val == string.Empty)
break;
dc.ColumnName = val;
ctr++;
}
ds.Tables["Data"].Rows.Remove(rowDel);
dtResult = ds.Tables["Data"];
DataTable dtCloned = dtResult.Clone();
dtCloned.Columns[1].DataType = typeof(string);
dtCloned.Columns[0].DataType = typeof(string);
foreach (DataRow row in dtResult.Rows)
{
dtCloned.ImportRow(row);
}
objConn.Close();
return dtCloned; //Returning Dattable
}
}
UnhandledException: System.AccessViolationException: Попытка чтения или записи в защищенной памяти. Это часто указывает на то, что другая память повреждена
в System.Data.Oledb.DataSourceWrapper.InitializeAndCreateSession (строка OledbConnection, SessionWrapper &