繁体   English   中英

查询未使用C#在MS Access中返回结果吗?

[英]Query is not returning results in MS Access with C#?

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

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

事实证明,添加表名是问题的原因。 为何会这样,尚不确定,我将继续讨论另一个针对此的问题。

因此,我最终使用的解决方案更改了以下内容:

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

傻,我知道。 现在感到愚蠢。

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

我希望这个帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM