当前上下文中不存在

发布于 2025-01-08 16:37:22 字数 993 浏览 5 评论 0 原文

为什么我的这行代码显示错误

 var searchUser = from accnt_user in dbo.Accounts
                             where accnt_user == txtUser.Text && 
                             accnt_Pass == txtPassword.Text
                             select accnt_user;

错误 2 名称“accnt_Pass”在当前上下文中不存在 C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\Login.cs 39 66 PAS

错误 1 ​​当前上下文中不存在名称“dbo”C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\Login.cs 38 49 PAS

但我已经将我的数据上下文链接到它

DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);

并且我的 DataClasses1DataContext 已经有这行代码

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Accounts")]
    public partial class Account : INotifyPropertyChanging, INotifyPropertyChanged

注意:我没有在数据上下文中添加所有代码,因为它很长

在此处输入图像描述

How come this line of code mine shows an error of

 var searchUser = from accnt_user in dbo.Accounts
                             where accnt_user == txtUser.Text && 
                             accnt_Pass == txtPassword.Text
                             select accnt_user;

Error 2 The name 'accnt_Pass' does not exist in the current context C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\Login.cs 39 66 PAS

Error 1 The name 'dbo' does not exist in the current context C:\Users\John\documents\visual studio 2010\Projects\PAS\PAS\Login.cs 38 49 PAS

But I already my datacontext linked to it

DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);

And My DataClasses1DataContext already have this line of code

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Accounts")]
    public partial class Account : INotifyPropertyChanging, INotifyPropertyChanged

NOTE: I have not added all of the code in the datacontext since it is long

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2025-01-15 16:37:22

尝试这样,因为 accnt_user 这将是 Accounts 的对象,因此您需要使用 . 运算符访问其所有成员

var searchUser = from accnt_user in Accounts
                             where accnt_user.accnt_user== txtUser.Text && 
                             accnt_user.accnt_pass== txtPassword.Text
                             select accnt_user;

try like this, as accnt_user this will be the object of Accounts, so you need to access its all member by using . operatorr

var searchUser = from accnt_user in Accounts
                             where accnt_user.accnt_user== txtUser.Text && 
                             accnt_user.accnt_pass== txtPassword.Text
                             select accnt_user;
离不开的别离 2025-01-15 16:37:22

您没有在查询中使用上下文。此外,所有列名称都必须以为源表或联接定义的别名作为前缀(与 SQL 不同,如果列在查询中的表或视图集中是唯一的,则可以省略前缀)。

将代码更改为如下所示:

using(DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
{
    var searchUser = from user in myDbContext.Accounts
                     where user.accnt_user == txtUser.Text && 
                     user.accnt_Pass == txtPassword.Text
                     select user;

    // do something with searchUser
}

这本质上相当于此 SQL:

select user.*

from dbo.Accounts user

where user.accnt_user = @user and user.accnt_pass = @pass

You aren't using the context in your query. Also, all column names must be prefixed by the alias defined for the source table or join (unlike SQL, where you can omit the prefix if the column is unique across the set of tables or views in the query).

Change your code to look like this:

using(DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
{
    var searchUser = from user in myDbContext.Accounts
                     where user.accnt_user == txtUser.Text && 
                     user.accnt_Pass == txtPassword.Text
                     select user;

    // do something with searchUser
}

This is essentially equivalent to this SQL:

select user.*

from dbo.Accounts user

where user.accnt_user = @user and user.accnt_pass = @pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文