Different record returned from SQL Server Management Studio and a C# ExecuteReader with the same exact query?

Alvord, Timothy 276 Reputation points
2025-02-26T17:32:50.5466667+00:00

I am using the EXACT same query in SQL Server Management Studio (2014) as I am in my C# program, but I get different results. How is this possible?

My Query:

SELECT tblWTHistory.Date, tblWTHistory.EstCompDate, tblOperator.BadgeID, tblOperator.OperatorName, tblWTHistory.WONum, tblWTHistory.WOType, tblWTHistory.PartNum, tblWTHistory.CellReceivedQty, tblWTHistory.OpNum,

                     tblWorkCenter.WorkCenterName, tblWorkType.WorkTypeName, tblWTHistory.WorkQty, tblWTStatus.StatusName, tblWTHistory.ReasonID, tblWTReasons.ReasonName, tblWTHistory.Rework, tblWTHistory.OpComment

FROM tblWTHistory INNER JOIN

                     tblWTReasons ON tblWTHistory.ReasonID = tblWTReasons.ReasonID INNER JOIN

                     tblWTStatus ON tblWTHistory.StatusID = tblWTStatus.StatusID INNER JOIN

                     tblWorkType ON tblWTHistory.WorkTypeID = tblWorkType.WorkTypeID INNER JOIN

                     tblWorkCenter ON tblWTHistory.WorkCenterID = tblWorkCenter.WorkCenterID INNER JOIN

                     tblOperator ON tblWTHistory.OperatorID = tblOperator.OperatorID

WHERE (tblWTHistory.CellReceivedQty > 0) AND (FORMAT(tblWTHistory.EstCompDate, 'yyyy-MM-dd') >= '2025-02-10') AND (FORMAT(tblWTHistory.EstCompDate, 'yyyy-MM-dd') <= '2025-02-13') AND (tblWTHistory.WOType = 'WO') AND

                     (tblWTHistory.WONum = '1858974')

ORDER BY tblWTHistory.PartNum, tblWTHistory.WONum, tblWTHistory.OpNum, tblWTHistory.StatusID, tblWTHistory.OperatorID, tblWTHistory.Date

C# Code:

string sSQL = "" Same query as above

SqlCommand myCmd = new SqlCommand(sSQL, myConnection);

SqlDataReader dr = myCmd.ExecuteReader();

When I loop through the dr I get 3 records while the "Results" pane in SSMS gives me the correct 4 records.

Anyone else ever run into a problem like this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,317 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,696 Reputation points
    2025-02-26T22:16:13.7666667+00:00

    I would guess either, they are not the same, probably one of the compare literals is different or the loop logic is wrong.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.