I have the following query which I am executing on an Access database. The query, when run in Access returns accurate results. However when run from the code I get back all of the items in the database, even those which fall outside the date range I am searching for.
I was wondering if the issue was because the parameter names are the same as the column names in the table, so I changed the parameter names @StartDate
and @EndDate
to be @FromDate
and @ToDate
and this fixed the problem, if the parameter names are different I get the right result set back. This concerns me because in the project that I am working on this pattern is duplicated all over the place. However I am using a parameter named @Id
to update the records by Id
(column name in db table) and this is working fine. Is this a weird edge case? Can anyone shed any light on this behaviour.
Apologies for the massive code sample, but in this case I think the whole method is needed.
public override AcademicCycleTransportCollection FetchForDateRange(DateTime startDate, DateTime endDate) {
const String query =
"PARAMETERS
" +
" @StartDate DATE,
" +
" @EndDate DATE
" +
" ;
" +
"SELECT
" +
" [AcademicCycle].[Id] AS [Id],
" +
" [AcademicCycle].[Name] AS [Name],
" +
" [AcademicCycle].[AcademicCycleCategoryId] AS [AcademicCycleCategoryId],
" +
" [AcademicCycle].[ParentAcademicCycleId] AS [ParentAcademicCycleId],
" +
" [AcademicCycle].[StartDate] AS [StartDate],
" +
" [AcademicCycle].[EndDate] AS [EndDate],
" +
" [AcademicCycle].[IsPerpetual] AS [IsPerpetual],
" +
" [AcademicCycle].[IsLocked] AS [IsLocked]
" +
"FROM
" +
" AcademicCycle
" +
"WHERE
" +
" (StartDate <= @EndDate AND EndDate >= @StartDate) OR
" +
" IsPerpetual <> 0";
AcademicCycleTransportCollection transportCollection = new AcademicCycleTransportCollection();
OleDbCommand _fetchForDateRangeCommand = null;
if (_fetchForDateRangeCommand == null) {
OleDbConnection connection = _parentDataConnection.Connection;
_fetchForDateRangeCommand = new OleDbCommand(query, connection);
_fetchForDateRangeCommand.Parameters.Add("@StartDate", OleDbType.Date);
_fetchForDateRangeCommand.Parameters.Add("@EndDate", OleDbType.Date);
}
_fetchForDateRangeCommand.Transaction = _parentDataConnection.Transaction;
_fetchForDateRangeCommand.Parameters["@StartDate"].Value = startDate;
_fetchForDateRangeCommand.Parameters["@EndDate"].Value = endDate;
using (OleDbDataReader dbReader = _fetchForDateRangeCommand.ExecuteReader()) {
NullableDataReader reader = new NullableDataReader(dbReader);
while (reader.Read()) {
AcademicCycleTransport transport = FillTransport(reader);
transportCollection.Add(transport);
}
if (!reader.IsClosed) {
reader.Close();
}
}
return transportCollection;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…