查询没有在C#中使用MS Access返回结果?

时间:2014-08-08 18:04:19

标签: c# sql ms-access

所以我有一个相对简单的查询,但它与full_name文本字段不匹配,即使我可以看到数据存在并且完全匹配?

我确保检查传入的fullName参数是否正确。我找不到任何文档验证我是否需要单引号,但没有它就会抛出错误。

有人有任何建议吗?

有关SQL语句的问题?

    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;
    }

从此按钮按下调用上述代码:

    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);
    }

这会过滤日期范围以及是否有全名。我确保名称有一个有效值。

2 个答案:

答案 0 :(得分:0)

事实证明,添加表名是导致问题的原因。为什么会这样,不确定,我会跟进另一个特定的问题。

所以解决方案我最终改变了以下内容:

    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.

修改

我更新了上面的正确行以删除单引号,我不小心留下了副本和粘贴(数字uno编码错误!)。

我还发现了为什么我一直在盯着它看几个小时的错误。事实证明,该表被称为“配置文件”,而不是“配置文件”,因此,当我删除表名时,它工作!

所以另一种解决方案是:

(fullName != null ? "profiles.full_name=@full_name AND " : "") 
傻,我知道。现在觉得很蠢。

答案 1 :(得分:0)

我认为问题出在这里,你需要删除&#39;&#39;来自full_name行:

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)"
            );

并执行此操作:

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)"
            );

我希望这个帮助