Bobby X
asked on
Reading Excel Spreadsheet using ADO.NET and DbDataReader
Hi,
Based on the code below, is there a way to get a total count of records that exist in the Excel worksheet (Cities)?
Thanks very much in advance!
Based on the code below, is there a way to get a total count of records that exist in the Excel worksheet (Cities)?
Thanks very much in advance!
string connectionString = @"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = connection.CreateCommand())
{
// Cities$ comes from the name of the worksheet
command.CommandText = "SELECT ID,City,State FROM [Cities$]";
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
Debug.WriteLine(dr["ID"].ToString());
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi ,
Try this...
Try this...
select (select SUM(1) from Cities) as 'Row Number', * from Cities;
This will help u more...
http://www.sqlteam.com/article/returning-a-row-number-in-a-query
Thanks,
TCS
http://www.sqlteam.com/article/returning-a-row-number-in-a-query
Thanks,
TCS
Thanks for the grade and points
ASKER
Also, how do I dynamically get a row number for each record of the Excel?