如何为 MS Access 编写区分大小写的查询? [英] How to write Case Sensitive Query for MS Access?
问题描述
我想知道区分大小写的 MS Access 选择查询.
I want to know the Select Query for MS Access with case sensitive.
我有两个 VitualMonitorName 值如下
I have two values for VitualMonitorName as below
VCode VirtualMonitorName
Row 1 (1, 'VM1');
Row 2 (2, 'Vm1');
这里两个值不同.
如果我写
"SELECT VCode FROM VirtualMaster WHERE VirtualMonitorName like '" + Vm1 + "'";
它只回复 VCode = 1.
It replies VCode = 1 Only.
推荐答案
您可以使用 StrComp()
函数和 vbBinaryCompare
进行区分大小写的比较.下面是来自立即窗口的示例,用于显示 StrComp()
的工作原理.有关详细信息,请参阅 Access 帮助主题.
You can use the StrComp()
function with vbBinaryCompare
for a case-sensitive comparison. Here is an example from the Immediate window to show how StrComp()
works. See the Access help topic for more details.
? StrComp("a", "A", vbBinaryCompare)
1
? StrComp("a", "A",vbTextCompare)
0
StrComp()
如果前两个参数计算结果相等,则返回 0,如果它们不相等,则返回 1 或 -1,如果任一参数为 Null,则返回 Null.
StrComp()
returns 0 if the first two arguments evaluate as equal, 1 or -1 if they are unequal, and Null if either argument is Null.
要在查询中使用该函数,请提供 vbBinaryCompare
常量的值 (0) 而不是其名称.
To use the function in a query, supply the vbBinaryCompare
constant's value (0) rather than its name.
SELECT VCode
FROM VirtualMaster
WHERE StrComp(VirtualMonitorName, "Vm1", 0) = 0;
如果其他应用程序使用较新的访问数据库引擎 (ACE") 驱动程序,则此方法也可用于来自其他应用程序的查询.比如下面的C#代码
This approach is also available to queries from other applications if they use the newer Access Database Engine ("ACE") drivers. For example, the following C# code
string myConnectionString =
@"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
@"Dbq=C:UsersPublicDatabase1.accdb;";
using (OdbcConnection con = new OdbcConnection(myConnectionString))
{
con.Open();
using (var cmd = new OdbcCommand())
{
cmd.Connection = con;
cmd.CommandText =
"SELECT COUNT(*) AS n FROM [VirtualMaster] " +
"WHERE StrComp([VirtualMonitorName],?,?) = 0";
cmd.Parameters.AddWithValue("?", "Vm1");
cmd.Parameters.Add("?", OdbcType.Int);
var vbCompareOptions = new Dictionary<string, int>()
{
{"vbBinaryCompare", 0},
{"vbTextCompare", 1}
};
string currentOption = "";
currentOption = "vbBinaryCompare";
cmd.Parameters[1].Value = vbCompareOptions[currentOption];
Console.WriteLine(
"{0} found {1} record(s)",
currentOption,
Convert.ToInt32(cmd.ExecuteScalar()));
currentOption = "vbTextCompare";
cmd.Parameters[1].Value = vbCompareOptions[currentOption];
Console.WriteLine(
"{0} found {1} record(s)",
currentOption,
Convert.ToInt32(cmd.ExecuteScalar()));
}
}
生产
vbBinaryCompare found 1 record(s)
vbTextCompare found 2 record(s)
这篇关于如何为 MS Access 编写区分大小写的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!