使用 C# 查询未在 MS Access 中返回结果? [英] Query is not returning results in MS Access with C#?
问题描述
所以我有一个相对简单的查询,但它不匹配 full_name
文本字段,即使我可以看到数据在那里并且完全匹配?
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?
我确保检查传入的 fullName
参数是否正确.我找不到任何文档来验证我是否需要单引号,但如果没有它,则会引发错误.
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.
大家有什么建议吗?
与 SQL 语句相关的代码?
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);
}
这会过滤日期范围以及是否有全名.我确保 name 有一个有效的值.
This filters the date range and if there is a full name. I ensure there is a valid value for name.
推荐答案
事实证明,在表名中添加是导致问题的原因.为什么会这样,尚不确定,我将跟进另一个具体的问题.
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)"
);
并将其更改为:
(fullName != null ? "full_name=@full_name AND " : "") + // Notice I removed the table name and just straight referenced the field.
我更新了上面正确的行以删除单引号,这是我在复制和粘贴时不小心留下的(numero uno 编码错误!).
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.
这篇关于使用 C# 查询未在 MS Access 中返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!