C# 使用C对Excel进行选择,如何使其不区分大小写?

C# 使用C对Excel进行选择,如何使其不区分大小写?,c#,sql-server,case-insensitive,lowercase,C#,Sql Server,Case Insensitive,Lowercase,此选择按预期查找Kelly: 从[Data$]中选择[First Name]、[Last Name],其中[First Name]类似于%Kelly% 在Excel电子表格中,第一个名字是Kelly,带大写字母K-,选择也指定大写字母K 但是,如果K in>like%Kelly%

此选择按预期查找Kelly:

从[Data$]中选择[First Name]、[Last Name],其中[First Name]类似于%Kelly%

在Excel电子表格中,第一个名字是Kelly,带大写字母K-,选择也指定大写字母K

但是,如果K in>like%Kelly%<是小写形式的%Kelly%,则找不到该记录。SELECT是区分大小写的

SQL Server似乎没有可以应用于数据库列的lower或lcase方法???!!!。这是真的吗?网络上的广泛建议是,在SQL语句中附加COLLATE SQL\u Latin1\u General\u CP1\u CI\u AS,执行ExecuteReader时会产生错误IErrorInfo.GetDescription失败0x80004005

有人能建议一种方法,让我的SQL选择不区分大小写吗

我已将代码粘贴到下面

当传递有效字符串时,f.vs方法返回true,即IsEmptyOrNull为false的字符串

蒂亚-霍伊特斯特


SQL Server确实有一个名为的函数,该函数将字符串转换为所有小写字母

[COLLATE SQL\u Latin1\u General\u CP1\u CI\u AS]仅在SQL Server中工作。从您的问题中我可以看出,您没有使用SQL Server;您正在使用OLEDB数据源查询Excel文件,在这种情况下,UCASE应该可以工作:

if (f.vs(firstName))
    select += "UCase([First Name]) like \"%" + firstName.ToUpper() + "%\" and ";
if (f.vs(lastName))
    select += "UCase([Last Name]) like \"%" + lastName.ToUpper() + "%\" and ";

成功了,非常感谢,肯!Excel不是SQL Server,是吗!:
        // Build the select; [Data$] is the name of the worksheet
        string select = "select [First Name], [Last Name], Phone from [Data$] where ";
        if (f.vs(firstName))
            select += "[First Name] like \"%" + firstName + "%\" and ";
        if (f.vs(lastName))
            select += "[Last Name] like \"%" + lastName + "%\" and ";
        if (f.vs(category))
            select += "[Category] = \"" + category + "\" and ";
        select = select.Substring(0, select.Length - 4); // Lose the terminal "and "

        // This makes the SQL case-insensitive! BUT IT CAUSES ExecuteReader() FAIL
        // select += " [COLLATE SQL_Latin1_General_CP1_CI_AS]";

        // Build the connect string, using the specified Excel address file
        string connectionString =
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
            @excelAddressFile +
            ";Extended Properties=Excel 8.0;";

        // Get a DB connection from an OLE DB provider factory
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        using (DbConnection connection = factory.CreateConnection())
        {

            // Assign the connection string
            connection.ConnectionString = connectionString;

            // Create the DB command
            using (DbCommand command = connection.CreateCommand())
            {
                // Apply the select
                command.CommandText = select;

                // Retrieve the data -- THIS ExecuteReader() IS WHERE IT FAILS
                using (DbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
if (f.vs(firstName))
    select += "UCase([First Name]) like \"%" + firstName.ToUpper() + "%\" and ";
if (f.vs(lastName))
    select += "UCase([Last Name]) like \"%" + lastName.ToUpper() + "%\" and ";