且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-07 23:21:22

事实证明,在表名中添加是导致问题的原因.为什么会这样,尚不确定,我将跟进另一个具体的问题.

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.