从以星号开头的表中搜索一行

时间:2014-04-28 14:00:37

标签: c#

我正在使用c#上传excel文件,我正在从该excel文件中选择一个名为LOT的列。在列LOT中,一行的编号以星号(*)开头。我需要一个查询来选择所有行。请帮帮我。

我尝试了下面的示例代码,但它运行不正常。它返回没有星号(*)的批次。

protected void btnUpload_Click(object sender, EventArgs e)
{
    //Get path from web.config file to upload
    string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();

    string filename = string.Empty;
    //To check whether file is selected or not to uplaod
    if (BrowseFile.HasFile)
    {
        try
        {
            string[] allowdFile = { ".xls", ".xlsx" };
            //Here we are allowing only excel file so verifying selected file pdf or not
            string FileExt = System.IO.Path.GetExtension(BrowseFile.PostedFile.FileName);
            //Check whether selected file is valid extension or not
            bool isValidFile = allowdFile.Contains(FileExt);
            if (!isValidFile)
            {
                lblMsg.ForeColor = System.Drawing.Color.Red;
                lblMsg.Text = "Please upload only Excel";
            }
            else
            {
                // Get size of uploaded file, here restricting size of file
                int FileSize = BrowseFile.PostedFile.ContentLength;
                if (FileSize <= 1048576)//1048576 byte = 1MB
                {
                    //Get file name of selected file
                    filename = Path.GetFileName(Server.MapPath(BrowseFile.FileName));

                    //Save selected file into server location
                    BrowseFile.SaveAs(Server.MapPath(FilePath) + filename);
                    //Get file path
                    string filePath = Server.MapPath(FilePath) + filename;
                    //Open the connection with excel file based on excel version
                    OleDbConnection con = null;
                    if (FileExt == ".xls")
                    {
                        con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");

                    }
                    else if (FileExt == ".xlsx")
                    {
                        con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;");
                    }
                    con.Open();
                    //Get the list of sheet available in excel sheet
                    DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    //Get first sheet name
                    string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
                    //Select rows from first sheet in excel sheet and fill into dataset
                    //OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT LOT FROM [" + getExcelSheetName + @"] WHERE LOT IS NOT NULL", con);
                    OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT LOT FROM [" + getExcelSheetName + @"] WHERE LOT LIKE '**'", con);
                    OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
                    DataTable ExcelDataSet = new DataTable();
                    ExcelAdapter.Fill(ExcelDataSet);
                    //Holding the data into the list
                    List<DataRow> strLot = ExcelDataSet.AsEnumerable().ToList();
                    con.Close();
                    //string lots = "";
                    //seperating the each lot with a comma separator
                    for (int i = 0; i < strLot.Count; i++)
                    {
                        totLots += ExcelDataSet.Rows[i].ItemArray.GetValue(0).ToString() + ",";
                    }
                    //Removing the last comma separator
                    totLots = totLots.Remove(totLots.Length - 1);

2 个答案:

答案 0 :(得分:1)

您可以使用LIKE运算符

  

WHERE <Column> LIKE '\**'

此外,根据您的数据来源,通配符'*'可以替换为'%',而'LIKE'关键字可以替换为'ALIKE'

'%'搜索文本将变为'*%'而不是'\**'

修改:使用.xls和.xlsx文件检查下面的代码

OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT LOT FROM [" + getExcelSheetName + @"] WHERE LOT LIKE '*%'", con);

答案 1 :(得分:0)

将命令文本更改为以下内容......

@"SELECT [LOT] FROM [" + getExcelSheetName + @"] WHERE [LOT] LIKE '\*%')"

这应该执行a simple pattern match like a SQL script并查找以星号开头并且之后包含任何内容的列的任何成员。