简体   繁体   English

在 c# 中对 MS Access 使用 StrComp 函数执行区分大小写的 sql 查询

[英]Perform a case-sensitive sql query with StrComp funtion for MS Access in c#

I am new to C#. I tried to write a case-sensitive sql query for Microsoft Access in C#. Here is part of the table named "EmployeeList".我是 C# 的新手。我尝试在 C# 中为 Microsoft Access 编写区分大小写的 sql 查询。这是名为“EmployeeList”的表的一部分。 Notice the second record has a lower case for "s":注意第二条记录的“s”是小写的:

| ID   | Name      | InternalID |
|:----:|:---------:| :---------:|
| 8    | John Smith| 52455      |
| 9    | John smith| 49         |
| ...  | ...       | ...        |

Here is the query.这是查询。 I tested in Access, and it worked well.我在 Access 中测试过,效果很好。

SELECT InternalID FROM EmployeeList WHERE StrComp(Name, 'John Smith', 0) = 0;

What is the correct syntax to write it in C#?在 C# 中写入它的正确语法是什么? I have looked up many sources but my code is still wrong:我查了很多资料,但我的代码仍然是错误的:

public DataTable findInternalID(string name)
        {
            Connection.Open();
            DataTable output = new DataTable();
            OleDbCommand bdCommand = RecordConnection.CreateCommand() as OleDbCommand;
            
            StringBuilder x = new StringBuilder();
            x.AppendLine("SELECT InternalID ");
            x.AppendLine("FROM EmployeeList ");
            x.AppendLine($"WHERE StrComp(Name, @name, 0) = 0");
          
            bdCommand.CommandText = x.ToString();
            bdCommand.Parameters.AddWithValue("name", $"%{name}%");

            _dataAdapter = new OleDbDataAdapter(bdCommand);
            _dataAdapter.Fill(output);

            RecordConnection.Close();
            return output;

        }

and here is the code under main to test my function:这是测试我的 function 的主要代码:

string name = "John Smith";
DataTable output = _accessManager.findInternalID(name);
String message;
message = output.Rows[0][0].ToString();
MessageBox.Show(message);

and this code throws me an error:这段代码给我一个错误:

System.IndexOutOfRangeException: 'There is no row at position 0.' System.IndexOutOfRangeException:“position 0 处没有行。”

Why there's no record written into result?为什么没有记录写入结果?

StrComp is an Access function, not a SQL Server function. StrComp是 Access function,而不是 SQL Server function。

So this will get rid of the error:所以这将消除错误:

commandTextBuilder.AppendLine($"WHERE Name = @name");

However, that's not case-sensitive.但是,这不区分大小写。

You can use StrComp if you use newer drivers, as per here:如果您使用较新的驱动程序,则可以使用StrComp ,如下所示:

How to write Case Sensitive Query for MS Access? 如何为 MS Access 编写区分大小写的查询?

If this were SQL Server, you'd want to change the column to be case-sensitive.如果这是 SQL 服务器,您需要将该列更改为区分大小写。 The command to do so looks something like这样做的命令看起来像

ALTER TABLE <mytable>
ALTER COLUMN <mycolumn> VARCHAR(<mylength>) 
COLLATE <myencoding>

Just fill in the blanks.只需填空即可。 myencoding will be whatever the current encoding is, with CI replaced with CS . myencoding将是当前编码的任何内容, CI替换为CS If you're running defaults, that means going from SQL_Latin1_General_CP1_CI_AS to SQL_Latin1_General_CP1_CS_AS .如果您正在运行默认值,则意味着从SQL_Latin1_General_CP1_CI_ASSQL_Latin1_General_CP1_CS_AS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM