Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

How to write Case Sensitive Query for MS Access?

I want to know the Select Query for MS Access with case sensitive.

I have two values for VitualMonitorName as below

VCode VirtualMonitorName
Row 1 (1, 'VM1');
Row 2 (2, 'Vm1');

Here both values are different.

If I write

"SELECT VCode FROM VirtualMaster WHERE VirtualMonitorName like '" + Vm1 + "'";

It replies VCode = 1 Only.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() returns 0 if the first two arguments evaluate as equal, 1 or -1 if they are unequal, and Null if either argument is Null.

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;

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()));
    }
}

produces

vbBinaryCompare found 1 record(s)
vbTextCompare found 2 record(s)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...