majkinetor
Programmer
Hi to all
I am loading excell file using ODBC connection. The excell file contains number of tables (sheets) that should be loaded to dataset. To connect to excell file using MS ODBC driver I set connection string so ODBC returns strings for eatch cell, no matter acctual type in excel file. This will also return column names in first row.
So, I need to load eatch sheet into DataTable, set the correct column names using data in first row and delete this first row after. After that table is added to dataset.
I am using this code to achive this:
This is the problem I have:
I am using XMLVisualizer plugin to check the tables and detaset in debug mode. When I check dt on point marked with <--- it shows me that table is correctly set. But, for some reason, I can't view xlsDS dataset. I tried after this operation expression:
but this one complians about accessing deleted datatable data. XMLVisualizer itself returns some error when trying to view this dataset. So I commented the part of above code when first row is deleted but that didn't help. When I comment part that change column names the error doesn't apper. So, it seems that there is some side effect of changing the colum names. Don't know why this has to do with DataSet as I add tables to it after all processing is done and viewing datatable alone shows it is set correctly.
Any help or tip would be highly appreciated.
Thx.
I am loading excell file using ODBC connection. The excell file contains number of tables (sheets) that should be loaded to dataset. To connect to excell file using MS ODBC driver I set connection string so ODBC returns strings for eatch cell, no matter acctual type in excel file. This will also return column names in first row.
So, I need to load eatch sheet into DataTable, set the correct column names using data in first row and delete this first row after. After that table is added to dataset.
I am using this code to achive this:
Code:
OleDbConnection conn = new OleDbConnection( XLS_GetExcellConnectionString(xlsFile) );
OleDbCommand cmnd = new OleDbCommand("", conn);
DbDataAdapterEx adapter = new DbDataAdapterEx() ;
OleDbDataReader reader;
conn.Open();
xlsDS = new DataSet();
foreach (string tableName in XLS_GetExcelSheetNames(xlsFile))
{
DataTable dt = new DataTable( tableName );
//read entire table
cmnd.CommandText = String.Format("SELECT * FROM [{0}]", tableName);
reader = cmnd.ExecuteReader();
adapter.FillFromReader ( dt, reader ) ;
reader.Close();
//set column names
foreach (DataColumn col in dt.Columns)
{
col.ColumnName = dt.Rows[0][col.Caption].ToString();
col.Caption = col.ColumnName;
}
dt.Rows[0].Delete(); // <---
xlsDS.Tables.Add(dt);
}
conn.Close();
This is the problem I have:
I am using XMLVisualizer plugin to check the tables and detaset in debug mode. When I check dt on point marked with <--- it shows me that table is correctly set. But, for some reason, I can't view xlsDS dataset. I tried after this operation expression:
Code:
string t = xlsDS.Tables[0].Rows[0][0].ToString();
Any help or tip would be highly appreciated.
Thx.