| re: How to access one value from a Excel sheet using a SQL statement and C#
I gave up on using the Last() function and made it get the whole column
private int GetLastLeadID()
{
//If there are no exixting id there is a 1 returned to start the table with
double lastid=0;
// Connect to the data source.
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_strSampleFolder +"LeadConsistencyScorecard.xls;Extended Properties=Excel 8.0;");
objConn.Open();
// Execute a command to retrieve all records from the Employees table.
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand(
"Select Lead_ID FROM [Leads$]", objConn);
System.Data.OleDb.OleDbDataReader objReader;
objReader = objCmd.ExecuteReader();
while (objReader.Read())
{
if (!objReader.IsDBNull(0) )
{
if(objReader.GetDataTypeName(0) == "DBTYPE_R8")
lastid = objReader.GetDouble(0);
if(objReader.GetDataTypeName(0) == "DBTYPE_WVARCHAR")
lastid = Convert.ToDouble(objReader.GetString(0));
}
}
// Close the connection.
objConn.Close();
return Convert.ToInt32(lastid);
}
|