[英]Using C# to SELECT against Excel, how do I make it case-INsensitive?
This SELECT finds Kelly as expected: 此SELECT可以按预期找到Kelly:
select [First Name], [Last Name], Phone from [Data$] where [First Name] like "%Kelly%" 从[Data $]中选择[名字],[姓氏],电话,其中[名字]如“%Kelly%”
In the Excel spreadsheet, the first name is "Kelly" with a capital "K" -- and the SELECT specifies a capital "K" also. 在Excel电子表格中,第一个名字是“ Kelly”,大写字母“ K”-SELECT也指定大写字母“ K”。
However, if the K in > like "%Kelly%" < is LOWER-case -- like "%kelly%" -- then the record is NOT found. 但是,如果K in>(例如“%Kelly%” <)是小写字母(例如“%kelly%”),则找不到该记录。 The SELECT is case-sensitive.
SELECT区分大小写。
SQL Server does not appear to have a lower() or lcase() method that I can apply to the database column (???!!!). SQL Server似乎没有我可以应用于数据库列(??? !!!)的lower()或lcase()方法。 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.
网上的广泛建议是,在执行ExecuteReader()时,将“ COLLATE SQL_Latin1_General_CP1_CI_AS”附加到SQL语句,会产生错误“ IErrorInfo.GetDescription失败0x80004005”。
Can someone please suggest a way to make my SQL SELECT against Excel case-INsensitive? 有人可以建议一种使我的SQL SELECT对Excel不区分大小写的方法吗?
I've pasted the code below. 我粘贴了下面的代码。
(The f.vs() method returns true when passed a Valid String, ie one for which IsEmptyOrNull() is false.) (f.vs()方法在传递有效字符串时返回true,即IsEmptyOrNull()为false的字符串。)
TIA - Hoytster 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())
{
SQL Server确实具有一个称为LOWER的函数,该函数会将字符串转换为所有小写字母
[COLLATE SQL_Latin1_General_CP1_CI_AS]
only works in SQL Server. [COLLATE SQL_Latin1_General_CP1_CI_AS]
仅在SQL Server中有效。 From what I can tell from your question, you're not using SQL Server; 从您的问题中可以看出,您没有使用SQL Server。 you're using an OLEDB data source to query an Excel file, in which case
UCASE
should work: 您正在使用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 ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.