我的应用程序和 Access 查询向导之间的不同 LIKE 行为 [英] Different LIKE behaviour between my application and the Access query wizard

查看:12
本文介绍了我的应用程序和 Access 查询向导之间的不同 LIKE 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的 Web 应用程序执行以下查询并访问 2007 查询向导.我得到了两种不同的结果.

I am executing following query from my web application and access 2007 query wizard. And I am getting two different result.

SELECT R.Rept_Name, D.Dist_Name,S.State_Name FROM (tblReporter AS R LEFT JOIN tblDist AS D ON R.Dist_Id=D.Dist_Id) LEFT JOIN  tblState AS S ON S.State_Id=R.State_Id WHERE R.Rept_Name LIKE '*Ra*' ORDER BY R.Rept_Name;

来自 Web 应用程序的结果为 0 行,来自查询向导的结果为 2 行.如果我删除 where 条件,则两个结果相同.请帮我查询有什么问题.如果需要任何其他信息,请告诉我.

Result from web application is with 0 rows and from query wizard 2 rows.If I remove where condition than both result are same. Please help me what is wrong with query. If any other info require please tell me.

Web 应用程序代码...

Web application code ...

public DataTable getRept(string rept, string mobno)
{
    DataTable dt = new DataTable();
    using (OleDbConnection conn = new OleDbConnection(getConnection()))
    {
        using (OleDbCommand cmd = conn.CreateCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT R.Rept_Name, D.Dist_Name,S.State_Name FROM (tblReporter AS R LEFT JOIN tblDist AS D ON R.Dist_Id=D.Dist_Id) LEFT JOIN  tblState AS S ON S.State_Id=R.State_Id WHERE R.Rept_Name LIKE '*" + rept + "*'  ORDER BY R.Rept_Name;";
            conn.Open();
            using (OleDbDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.HasRows)
                    dt.Load(sdr);
            }

        }
    }
    return dt;
}

推荐答案

您会被在 Access 本身中运行的查询和从外部应用程序运行的查询之间 LIKE 通配符的差异所困扰.

You are getting tripped up by the difference in LIKE wildcard characters between queries run in Access itself and queries run from an external application.

从 Access 本身运行查询时,您需要使用星号作为通配符:LIKE '*Ra*'.

When running a query from within Access itself you need to use the asterisk as the wildcard character: LIKE '*Ra*'.

从外部应用程序(如 C# 应用程序)运行查询时,您需要使用百分号作为通配符:LIKE '%Ra%'.

When running a query from an external application (like your C# app) you need to use the percent sign as the wildcard character: LIKE '%Ra%'.

这篇关于我的应用程序和 Access 查询向导之间的不同 LIKE 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆