简体   繁体   English

在Excel试算表的SQL查询中包含字串的问题

[英]Trouble including a string in an SQL query for an Excel Spreadsheet

I am displaying data from an Excel Spreadsheet through an ASP.net web form using C#. 我正在使用C#通过ASP.net Web表单从Excel电子表格显示数据。 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. 我想对数据运行SQL查询,但是在弄清楚如何在查询中使用字符串时遇到了麻烦。

Here is the code I am running in my .aspx.cs file. 这是我在.aspx.cs文件中运行的代码。 I am also using a .aspx to display the data in a GridView . 我还使用.aspx在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]"; 理想情况下,我想在我的string sSQL = "SELECT * FROM [Sheet1$A1:D14]";添加WHERE子句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. 为了查询当前月份并从Excel电子表格中显示该月份的行。

I have rewritten your code for you, however, you should seriously consider sticking into some coding standards. 我已经为您重写了代码,但是,您应该认真考虑遵守某些编码标准。 Please don't end your variables with numbers and consider formatting your code then it's more readable. 请不要以数字结尾变量,并考虑格式化代码,这样可读性更好。 Also you need to surround your OleDbConnection() inside a using statement then it gets disposed properly. 另外,您需要将OleDbConnection()包围在using语句内,然后对其进行正确处理。

Here is the formatted and reworked code 这是格式化和重做的代码

public partial class ExcelAdapter : System.Web.UI.Page
{
    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\"";

        using (OleDbConnection objConn = new OleDbConnection(sConnectionString))
        {
            objConn.Open();
            var sSQL = "SELECT * FROM [Sheet1$A1:D14]";
            OleDbCommand objCmdSelect = new OleDbCommand(sSQL, objConn);
            objCmdSelect.CommandType = CommandType.Text;
            DataSet objDataset = new DataSet();
            OleDbDataAdapter objAdapter = new OleDbDataAdapter(objCmdSelect).Fill(objDataset);
            objConn.Close();
        }
    }
}

I haven't had time to test this code but I am pretty sure it should be spot on. 我还没有时间测试这段代码,但是我很确定它应该是正确的。 (I hope) (我希望)

Further more have a look at this tutorial. 更多内容请看本教程。 It's an excellent source to give you an idea of how to do this job properly. 这是让您了解如何正确完成此工作的绝佳资源。

Import Excel File to DataSet 将Excel文件导入数据集

Where Clause 凡条款

It's very easy to add the where clause to your SQL and even better you can parametrize it to make it more standard. where子句添加到SQL中非常容易,甚至更好的是,可以对其进行参数化以使其更加标准。

Your code should look something like 您的代码应类似于

    var sSQL = "SELECT * FROM [Sheet1$A1:D14] WHERE currentDate = ?";
    cmd.Parameter.Add("@Param1", OleDbType.VarChar).Value = todaysDate;

For the full example of how the parametrization should be done have a look at OleDbCommand.Parameters Property and also for even more details on an example you can check my answer's example to this question Missing Required Parameter in Parameterized Query? 有关应如何进行参数化的完整示例,请查看OleDbCommand.Parameters属性 ,有关示例的更多详细信息,可以查看我对这个问题的答案示例,是否存在参数化查询中的缺少必需参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM