0

此 SELECT 按预期找到 Kelly:

选择 [First Name]、[Last Name]、Phone from [Data$] where [First Name] like "%Kelly%"

在 Excel 电子表格中,名字是大写“K”的“Kelly”——SELECT 还指定了大写“K”。

但是,如果 > 中的 K 像 "%Kelly%" < 是小写的——比如 "%kelly%"——那么就找不到记录。SELECT 区分大小写。

SQL Server 似乎没有可以应用于数据库列的 lower() 或 lcase() 方法(???!!!)。这是真的吗?网络上的广泛建议是将“COLLATE SQL_Latin1_General_CP1_CI_AS”附加到 SQL 语句,执行 ExecuteReader() 时会产生错误“IErrorInfo.GetDescription failed 0x80004005”。

有人可以建议一种方法让我的 SQL SELECT 对 Excel 不区分大小写吗?

我已经粘贴了下面的代码。

(f.vs() 方法在传递一个有效字符串时返回 true,即 IsEmptyOrNull() 为 false 的字符串。)

TIA - 霍伊斯特

        // The user may specify the first or last names, or category, or 
        // any combination, possibly all three.
        // 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())
                    {
4

2 回答 2

1

SQL Server 确实有一个名为LOWER的函数,它将字符串转换为全小写

于 2009-10-10T00:15:17.820 回答
1

[COLLATE SQL_Latin1_General_CP1_CI_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 ";
于 2009-10-10T23:30:56.800 回答