问题
So I have a relatively simple query, but it is not matching the full_name
text field even though I can see the data is there and is an exact match?
I made sure to checkk the fullName
parameter being passed in is the correct value. I couldn't find any documentation verifying if I needed the single quotes or not, but without it an error was thrown.
Anyone have any suggestions?
Code in question with the SQL statement?
public static ObservableCollection<ShiftView> GetShifts(DateTime? start, DateTime? stop, string fullName, bool? closed)
{
ObservableCollection<ShiftView> shifts = new ObservableCollection<ShiftView>();
// INFO: Not intended to retrieve a lot of records as there is no caching....
OleDbCommand cmd = new OleDbCommand("SELECT profiles.profile_id, profiles.full_name, shifts.start, shifts.stop, shifts.start_log, shifts.stop_log, shifts.start_notes, shifts.stop_notes FROM shifts, profiles WHERE " +
(start.HasValue ? "(shifts.start>=@start) AND " : "") +
(stop.HasValue ? "(shifts.stop<=@stop) AND " : "") +
(fullName != null ? "profile.full_name='@full_name' AND " : "") +
(closed.HasValue ? "shifts.closed=@closed AND " : "") +
"(shifts.profile_id=profiles.profile_id)"
);
if (start.HasValue)
cmd.Parameters.AddWithValue("@start", start.Value.ToString());
if (stop.HasValue)
cmd.Parameters.AddWithValue("@stop", stop.Value.ToString());
if (fullName != null)
cmd.Parameters.AddWithValue("@full_name", fullName);
if (closed.HasValue)
cmd.Parameters.AddWithValue("@closed", closed);
OleDbDataReader reader = Database.Read(cmd);
DateTime? _stop, _stopLog;
while(reader.Read())
{
Console.WriteLine("!");
// shorthand form if's with ? does not work here.
if (reader.IsDBNull(reader.GetOrdinal("stop")))
_stop = null;
else
_stop = reader.GetDateTime(reader.GetOrdinal("stop"));
if (reader.IsDBNull(reader.GetOrdinal("stop_log")))
_stopLog = null;
else
_stopLog = reader.GetDateTime(reader.GetOrdinal("stop_log"));
shifts.Add(new ShiftView(
reader.GetString(reader.GetOrdinal("profile_id")),
reader.GetString(reader.GetOrdinal("full_name")),
reader.GetDateTime(reader.GetOrdinal("start")),
_stop,
reader.GetDateTime(reader.GetOrdinal("start_log")),
_stopLog,
reader.GetString(reader.GetOrdinal("start_notes")),
reader.GetString(reader.GetOrdinal("stop_notes"))
));
}
return shifts;
}
The above code gets called from this button press:
private void ShowStatsButton_Click(object sender, RoutedEventArgs e)
{
DateTime? start = StartDatePicker.SelectedDate;
DateTime? stop = StopDatePicker.SelectedDate;
string name = NameComboBox.Text;
if (name.Equals("Everyone"))
name = null;
if (stop.HasValue)
stop = stop.Value.AddDays(1);
StatsGridView.ItemsSource = Shift.GetShifts(start, stop, name, true);
}
This filters the date range and if there is a full name. I ensure there is a valid value for name.
回答1:
I think that the problem is in here, you need to remove '' from full_name line:
OleDbCommand cmd = new OleDbCommand("SELECT profiles.profile_id, profiles.full_name, shifts.start, shifts.stop, shifts.start_log, shifts.stop_log, shifts.start_notes, shifts.stop_notes FROM shifts, profiles WHERE " +
(start.HasValue ? "(shifts.start>=@start) AND " : "") +
(stop.HasValue ? "(shifts.stop<=@stop) AND " : "") +
(fullName != null ? "profile.full_name='@full_name' AND " : "") + <-- remove '' from '@full_name'
(closed.HasValue ? "shifts.closed=@closed AND " : "") +
"(shifts.profile_id=profiles.profile_id)"
);
and do this:
OleDbCommand cmd = new OleDbCommand("SELECT profiles.profile_id, profiles.full_name, shifts.start, shifts.stop, shifts.start_log, shifts.stop_log, shifts.start_notes, shifts.stop_notes FROM shifts, profiles WHERE " +
(start.HasValue ? "(shifts.start>=@start) AND " : "") +
(stop.HasValue ? "(shifts.stop<=@stop) AND " : "") +
(fullName != null ? "profile.full_name=@full_name AND " : "") +
(closed.HasValue ? "shifts.closed=@closed AND " : "") +
"(shifts.profile_id=profiles.profile_id)"
);
I hope this help
回答2:
It turns out that adding in the table name was the cause of the problem. Why this is the case, is uncertain and I will follow up with another question specific to that.
So the solution I eventually went with to change the following:
OleDbCommand cmd = new OleDbCommand("SELECT profiles.profile_id, profiles.full_name, shifts.start, shifts.stop, shifts.start_log, shifts.stop_log, shifts.start_notes, shifts.stop_notes FROM shifts, profiles WHERE " +
(start.HasValue ? "(shifts.start>=@start) AND " : "") +
(stop.HasValue ? "(shifts.stop<=@stop) AND " : "") +
(fullName != null ? "profile.full_name='@full_name' AND " : "") + // this is where the problem is
(closed.HasValue ? "shifts.closed=@closed AND " : "") +
"(shifts.profile_id=profiles.profile_id)"
);
and change it to:
(fullName != null ? "full_name=@full_name AND " : "") + // Notice I removed the table name and just straight referenced the field.
EDIT:
I updated the above correct line to remove single quotes, that I accidentally left in copy and pasting (numero uno coding mistake!).
I also discovered why I kept getting an error staring at it for hours. Turns out, the table is called "profiles", not "profile" and therefore when I removed the table name it worked!
So an alternative solution is:
(fullName != null ? "profiles.full_name=@full_name AND " : "")
Silly, I know. Feel stupid now.
来源:https://stackoverflow.com/questions/25209593/query-is-not-returning-results-in-ms-access-with-c