【问题标题】:Query is not returning results in MS Access with C#?使用 C# 在 MS Access 中查询没有返回结果?
【发布时间】:2014-08-08 18:04:19
【问题描述】:

所以我有一个相对简单的查询,但它与 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);
    }

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

【问题讨论】:

    标签: c# sql ms-access


    【解决方案1】:

    我认为问题出在此处,您需要从 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)"
                );
    

    希望对你有帮助

    【讨论】:

    • 实际上,我发现了真正的问题,并将用它更新我的帖子。简而言之,在编辑答案时,我不小心留下了单引号。正如您指出的那样,应该将其删除。有第二个错误,但我错过了它盯着它几个小时。
    • 很高兴您发现问题并提供帮助;)
    • 感谢您的帮助!
    【解决方案2】:

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

    所以我最终采用的解决方案是更改以下内容:

        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 编码错误!)。

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

    所以另一种解决方案是:

    (fullName != null ? "profiles.full_name=@full_name AND " : "") 
    

    傻,我知道。现在觉得自己很傻。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2021-10-30
      相关资源
      最近更新 更多