I am displaying data from an Excel Spreadsheet through an ASP.net web form using C#. I would like to run an SQL query on the data, but am having trouble figuring out how to use a string in my query.
Here is the code I am running in my .aspx.cs file. I am also using a .aspx to display the data in a GridView
.
protected void Page_Load(object sender, EventArgs e)
{
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("ExcelCSTest.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
string sSQL = "SELECT * FROM [Sheet1$A1:D14]";
OleDbCommand objCmdSelect = new OleDbCommand(sSQL, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
GridView1.DataSource = objDataset1.Tables[0].DefaultView;
GridView1.DataBind();
objConn.Close();
}
Ideally, I would like to add a WHERE
clause to my string sSQL = "SELECT * FROM [Sheet1$A1:D14]";
in order to query the current month and display the row of said month from the Excel Spreadsheet.