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

发布于 2024-08-06 19:39:34 字数 2407 浏览 5 评论 0原文

此 SELECT 按预期找到 Kelly:

从 [Data$] 选择 [名字]、[姓氏]、电话,其中 [名字] 像“%Kelly%”

在 Excel 电子表格中,名字是大写的“Kelly” “K”——并且 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 - Hoytster

 // 用户可以指定名字或姓氏,或类别,或 
        // 任何组合,可能全部三个。
        // 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())
                    {

This SELECT finds Kelly as expected:

select [First Name], [Last Name], Phone from [Data$] where [First Name] like "%Kelly%"

In the Excel spreadsheet, the first name is "Kelly" with a capital "K" -- and the SELECT specifies a capital "K" also.

However, if the K in > like "%Kelly%" < is LOWER-case -- like "%kelly%" -- then the record is NOT found. The SELECT is case-sensitive.

SQL Server does not appear to have a lower() or lcase() method that I can apply to the database column (???!!!). Is that actually true? Widespread advice on the net, to append "COLLATE SQL_Latin1_General_CP1_CI_AS" to the SQL statement, produces the error "IErrorInfo.GetDescription failed 0x80004005" when ExecuteReader() is executed.

Can someone please suggest a way to make my SQL SELECT against Excel case-INsensitive?

I've pasted the code below.

(The f.vs() method returns true when passed a Valid String, i.e. one for which IsEmptyOrNull() is false.)

TIA - Hoytster

        // 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())
                    {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-08-13 19:39:34

[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 ";

[COLLATE SQL_Latin1_General_CP1_CI_AS] only works in SQL Server. From what I can tell from your question, you're not using SQL Server; you're using an OLEDB data source to query an Excel file, in which case UCASE should work:

if (f.vs(firstName))
    select += "UCase([First Name]) like \"%" + firstName.ToUpper() + "%\" and ";
if (f.vs(lastName))
    select += "UCase([Last Name]) like \"%" + lastName.ToUpper() + "%\" and ";
极致的悲 2024-08-13 19:39:34

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

SQL Server does have a function called LOWER that will convert a string of characters to all lowercase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文