Pedram, sure you can. Go ahead and use GetOleDbSchemaTable()
to get the details.
This is the way you proceed,
DataSet output = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
// e.g. Rows in SchemaTable are Sheets in Xlsx file.
foreach (DataRow schemaRow in schemaTable.Rows)
{
//...
string sheet = schemaRow["TABLE_NAME"].ToString();
var select = "SELECT * FROM [" + sheet + "]";
OleDbCommand cmd = new OleDbCommand(select, conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
}
Later on when you got your DataSet
you can get your DataTable
s out of it using myDataSet.Tables
, however they come in a DataTableCollection
type which only support Foreach
and not the IEnumerable()
then you could do LINQ queries on it without the need for iteration. .NET developers haven't decided to implement this due to multiple reasons you can read extensively over here.
However there is a very easy workaround and that's Casting it. So if you don't want to iterate over the collection and cherry pick the entries you want go ahead and use the following syntax in Method Syntax:
var foo = dataSet.Tables.Cast<DataTable>().Where(
t => t.TableName == "The Table I want");
or in the Query syntax LINQ form as,
var bar = from DataTable t in dataSet.Tables
where t.TableName == "The table I want"
select t;
Hope I understood you right though. I spent a fair bit of time figuring this out myself since I was curious.
Update
Bulk Copy
If you want to copy your whole table over OleDbConnection
to implement something like SqlBulkCopy
does in SqlConnection
you'll be looking to use IRowsetFastLoad
. You didn't mention if you're using Ado.Net or just the pure OleDb. If it's the first option and you're using C# unfortunately IRowsetFastLoad
is not implemented in this platform and only on C++. In this situation you are looking at iterating and grinding your teeth.
On the other hand if SqlBulkCopy
is an option, I am pretty sure you're already all over it. If not, have a quick look at the following article. It clears lots of things up.
Using SqlBulkCopy to efficiently copy table from Access to SQL Server
I had a quick Googling and came across solutions such as using adapter.Update()
, however I haven't used it this way before and won't be able to comment on it without testing.
DbDataAdapter.Update Method (DataTable)
Probably it worth having a look. Sorry, for the half way answer though. I had to go through your comments to fully understand what you want to accomplish.