简体   繁体   English

来自C#的MS Access SQL LIKE查询

[英]MS Access SQL LIKE query from C#

I have this query for Ms Access and im using C# and Ole DB commands. 我有Ms Access的查询,我使用C#和Ole DB命令。 It works on Ms Access but when I'm passing the query from C# using OleDB, nothing happened. 它适用于Ms Access,但是当我使用OleDB从C#传递查询时,没有任何反应。 Anyway here's my code: 无论如何这是我的代码:

SQL query SQL查询

SELECT * FROM tblIssue WHERE  id LIKE '*2*' AND dateChecque LIKE '**'AND +
issueTo LIKE '**' AND byTheName LIKE '**' AND bankName LIKE '**' AND accountNo LIKE '**' +
AND checqueNo LIKE '**' AND amount LIKE '**' AND being LIKE '**'   AND whoDeleted LIKE '**' +
AND whyDeleted LIKE '**' AND dateCreated LIKE '**';

C# code C#代码

try
{
    DataTable newDt = new DataTable();
    OleDbDataAdapter newSda = new OleDbDataAdapter(sqlQuery , conn);
    newSda.Fill(newDt);

    if (newDt.Rows.Count > 0)
    {
        dataGridView1.DataSource = newDt.DefaultView;
        _hasData = true;
    }
    else
    {
        _hasData = false;
    }
}
catch (Exception error)
{
    MessageBox.Show(error.ToString()); conn.Close();
}

Queries performed from within the Microsoft Access application itself normally use * and ? 从Microsoft Access应用程序本身执行的查询通常使用*? as wildcard characters for the LIKE operator. 作为LIKE运算符的通配符。 OleDb connections to an Access database from an external application should use the % and _ wildcard characters instead. 从外部应用程序到Access数据库的OleDb连接应该使用%_通配符。 (The latter are actually the more commonly-used wildcard characters in other SQL dialects.) (后者实际上是其他SQL方言中比较常用的通配符。)

From http://technet.microsoft.com/en-us/library/cc966377.aspx : 来自http://technet.microsoft.com/en-us/library/cc966377.aspx

Microsoft Jet uses partial match (or "wildcard") characters with the Like operator that are different from those used in most SQL dialects. Microsoft Jet使用与Like运算符不同的部分匹配(或“通配符”)字符,这些字符与大多数SQL方言中使用的字符不同。 The asterisk (*) character matches zero or more characters and is equivalent to the percent (%) character in ANSI SQL. 星号(*)字符与零个或多个字符匹配,相当于ANSI SQL中的百分比(%)字符。 The other Microsoft Jet partial match characters are the question mark (?), which matches any character in a single field, and the number sign (#), which matches any digit in a single field. 其他Microsoft Jet部分匹配字符是问号(?),它匹配单个字段中的任何字符,数字符号(#)匹配单个字段中的任何数字。

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

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