0

I have a table with feilds like

TimeStamp     |  Feild1      | Feild 2
--------------------------------------
1902909002    |  xyddtz      | 233447
1902901003    |  xytzff      | 233442
1902901105    |  xytzdd      | 233443
1902909007    |  xytzdd      | 233443
1902909909    |  xytsqz      | 233436

Now i want to query it and fetch records like timestamp = 1902909002 approx OR 1902901003 approx OR ... I want i record for each epoch time which ever is nearest to that epoch

i have written something :

 string sqlQuery = "SELECT TimeStamp, FwdHr, W FROM Meter_Data WHERE (TimeStamp <= "+timeSt[0].ToString();
                    for (int i = 1; i < timeSt.Count; i++)
                    {
                        sqlQuery = sqlQuery + " OR TimeStamp <= " +timeSt[i].ToString() ;
                    }

                    sqlQuery=sqlQuery+ ") AND MeterID = @meterID AND DeviceID = @deviceID ORDER BY TimeStamp";

which is returning null and also if from date and to date have large diff OR's will be in thousands. can any body suggest a better way?

4

1 回答 1

0

你能申请一个范围吗?我的意思是这样的:

var max=timeSt.Max();
var min=timeSt.Min();


var sqlString= string.Format(@"
SELECT 
    TimeStamp, 
    FwdHr, 
W 
    FROM 
    Meter_Data 
WHERE
    TimeStamp BETWEEN {0} AND {1}
    AND MeterID = @meterID AND DeviceID = @deviceID 
ORDER BY 
    TimeStamp",min,max);
于 2013-06-04T07:36:59.423 回答