简体   繁体   English

C#查询未返回任何内容

[英]c# query is not returning anything

here is my query: 这是我的查询:

select reporttime, datapath, finalconc, instrument from batchinfo 
    join qvalues on batchinfo.rowid=qvalues.rowid where qvalues.rowid 
    in (select rowid from batchinfo where instrument LIKE '%TF1%' and reporttime
    like '10/%/2010%') and compound='ETG' and  name='QC1'

i am running it like this: 我正在这样运行:

    // Create a database connection object using the connection string    
    OleDbConnection myConnection = new OleDbConnection(myConnectionString);

    // Create a database command on the connection using query    
    OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);

it does not return any results. 它不返回任何结果。

when i try this same query in sql server GUI it returns lots of rows 当我在sql server GUI中尝试相同的查询时,它返回很多行

is there a problem specifically with the syntax of the query for c#? c#查询的语法是否存在问题?

please note that if i simplify the query like select * from table , there is no issue 请注意,如果我简化查询,例如select * from table ,则没有问题

Can you try using SqlCommand instead of OleDbCommand ? 您可以尝试使用SqlCommand代替OleDbCommand吗?

According to MSDN it: Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. 根据MSDN,它: Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database.

So if you really are using SQL Server 2008 you should probably use this. 因此,如果您确实在使用SQL Server 2008,则应该使用它。 Any reason you are not? 您不是什么原因?

How are you setting up the mySelectQuery string? 您如何设置mySelectQuery字符串? Could this be a problem with an escape character? 这可能是转义字符有问题吗?

If you're not doing it already, 如果您还没有这样做,

string mySelectQuery = @"query text here";

我个人将运行查询事件探查器以查看正在运行的查询(如果有),然后从那里开始。

Are you sure you are connecting to the same database in your code? 您确定要在代码中连接到相同的数据库吗?

On a side note do you need the inner select? 另外,您需要内部选择吗? Couldn't you write this query as follows. 您不能按以下方式编写此查询。

select reporttime,
         datapath,
         finalconc, 
         instrument 
    from batchinfo  
           join qvalues on batchinfo.rowid = qvalues.rowid
   where compound = 'ETG' 
     and name = 'QC1'
     and batchinfo.instrument like '%TF1%'
     and batchinfo.reporttime like '10/%/2010%'

Edit - Never mind, just read the comment that your date is in a varchar field. 编辑-没关系,只需阅读一下您的日期在varchar字段中的注释即可。 Will leave this here since it may still be useful information. 将其保留在此处,因为它可能仍然是有用的信息。

Try this: 尝试这个:

reporttime like 'Oct%2010%'

I have a test table here and when I query it using 我在这里有一个测试表,当我使用它查询时

where LastModified like '11/%/2010%'

no rows are returned, although all of the rows in the table have dates in November 2010. When I run the same query using like 'Nov%2010%' , it returns all rows. 尽管表中的所有行都有日期都是2010年11月,但没有返回行。当我使用like 'Nov%2010%'运行相同的查询时,它将返回所有行。

Details can be found here : 详细信息可以在这里找到:

The LIKE clause can also be used to search for particular dates, as well. LIKE子句也可以用于搜索特定日期。 You need to remember that the LIKE clause is used to search character strings. 您需要记住,LIKE子句用于搜索字符串。 Because of this the value which you are searching for will need to be represented in the format of an alphabetic date. 因此,您要搜索的值将需要以字母日期格式表示。 The correct format to use is: MON DD YYYY HH:MM:SS.MMMAM, where MON is the month abbreviation, DD is the day, YYYY is the year, HH is hours, MM is minutes, SS is seconds, and MMM is milliseconds, and AM designates either AM or PM. 使用的正确格式是:MON DD YYYY HH:MM:SS.MMMAM,其中MON是月份的缩写,DD是日期,YYYY是年份,HH是小时,MM是分钟,SS是秒,而MMM是毫秒,AM表示AM或PM。

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

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