繁体   English   中英

C#查询未返回任何内容

[英]c# query is not returning anything

这是我的查询:

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'

我正在这样运行:

    // 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);

它不返回任何结果。

当我在sql server GUI中尝试相同的查询时,它返回很多行

c#查询的语法是否存在问题?

请注意,如果我简化查询,例如select * from table ,则没有问题

您可以尝试使用SqlCommand代替OleDbCommand吗?

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

因此,如果您确实在使用SQL Server 2008,则应该使用它。 您不是什么原因?

您如何设置mySelectQuery字符串? 这可能是转义字符有问题吗?

如果您还没有这样做,

string mySelectQuery = @"query text here";

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

您确定要在代码中连接到相同的数据库吗?

另外,您需要内部选择吗? 您不能按以下方式编写此查询。

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%'

编辑-没关系,只需阅读一下您的日期在varchar字段中的注释即可。 将其保留在此处,因为它可能仍然是有用的信息。

尝试这个:

reporttime like 'Oct%2010%'

我在这里有一个测试表,当我使用它查询时

where LastModified like '11/%/2010%'

尽管表中的所有行都有日期都是2010年11月,但没有返回行。当我使用like 'Nov%2010%'运行相同的查询时,它将返回所有行。

详细信息可以在这里找到:

LIKE子句也可以用于搜索特定日期。 您需要记住,LIKE子句用于搜索字符串。 因此,您要搜索的值将需要以字母日期格式表示。 使用的正确格式是: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