【问题标题】:Table does not exist in the current context?当前上下文中不存在表?
【发布时间】:2020-07-31 00:37:31
【问题描述】:

我需要在我的项目中管理来自不同数据库的许多表,无论是直接从它创建,还是在 UI 中(例如在 DGV 中)显示,以供读取、过滤和打印。在上一个问题中,它建议我使用 DataSets 和 LINQ,所以我搜索并遵循了通过代码添加 DataSet 的示例:

private void button1_Click(object sender, EventArgs e)
{
    openConnections(); // open connections for SqlCommands I'm using in this piece of code

    DataSet ds = new DataSet();

    SqlCommand command;
    command = new SqlCommand("SELECT * FROM [database1].[dbo].[articles]", connectionSQLDatabase1);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;
    adapter.Fill(ds, "articles"); //articles becomes the Tables[0] in the ds DataSet

    SqlCommand command2;
    command2 = new SqlCommand("SELECT * FROM [database2].[dbo].[articles_Details]", connectionSQLDatabase2);
    SqlDataAdapter adapter2 = new SqlDataAdapter();
    adapter2.SelectCommand = command2;
    adapter2.Fill(ds, "articles_Details"); //articles becomes the Tables[1] in the ds DataSet

    // LINQ code to query ds DataSet (.....)
}

现在我需要查询我的 ds 数据集的代码,我已经看到我可以使用 LINQ 来完成,但是我看到的几乎所有 LINQ 示例的结构都以我不认为的方式与我的代码匹配。

我期待编写 LINQ 代码,例如(来自网络的示例):

var query = from person in people
                 join pet in pets on person equals pet.Owner
                 select new { OwnerName = person.FirstName, PetName = pet.Name };

但我不能这样写表名而不会出错:

表在当前上下文中不存在。

我想知道我做错了什么,在全局项目中我必须在我的数据集中添加来自不同数据库的各种表,然后执行 INNER、LEFT 和 RIGTH JOINS 以及许多其他数据操作,在代码中直接控制表格将非常有用,就像最后一个示例似乎给出的那样。将此数据集添加到我的项目中我已经正确地遵循了一个示例,它是正确的,导致调试我的 ds 的内容我得到了我的表的全部内容。

希望有人能提供帮助。

【问题讨论】:

  • 首先阅读:LinQ vs Linq2SQL stackoverflow.com/a/1081932/6560478
  • 好吧,也许我明白了。现在我正在尝试通过 Visual Studio 过程将新数据源添加到我的项目中。我去code Project ->Add new data source->Database->Dataset\code 在这里我看到了标有“指定应用程序用于连接数据库的数据连接”的组合,但它只包含3个引用相对于我拥有的 6 个数据库中的 3 个的连接字符串。如果我使用“新建连接”按钮,它会打开“添加连接”表单,但搜索其他数据库却找不到任何东西。我应该管理一些参数来让我的所有数据库都显示在那里吗?
  • 请通过编辑而非 cmets 进行澄清。请在代码问题中给出minimal reproducible example--cut & paste & runnable code;具有期望和实际输出(包括逐字错误消息)的示例输入(作为初始化代码);标签和版本;明确的规范和解释。对于包含最少代码的错误,您可以给出的代码是您显示的代码可以通过您显示的代码扩展为不正常。 (调试基础。)对于 SQL 包括 DDL 和表格初始化。隔离第一个错误的子表达式及其输入和输出。 (调试基础。)

标签: c# linq join datatable dataset


【解决方案1】:

好的,因为我在我的案例中通过 LINQ 请求 INNER JOIN 方式并且我找到了解决方案,所以我发布它希望对某人有所帮助:

private void button1_Click(object sender, EventArgs e)

{ 
    openConnections(); // open connections for SqlCommands I'm using in this piece of code

    DataSet ds = new DataSet();

    SqlCommand command;
    command = new SqlCommand("SELECT * FROM [database1].[dbo].[articles]", connectionSQLDatabase1);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;
    adapter.Fill(ds, "articles"); //articles becomes the Tables[0] in the ds DataSet

    SqlCommand command2;
    command2 = new SqlCommand("SELECT * FROM [database2].[dbo].[articles_Details]", connectionSQLDatabase2);
    SqlDataAdapter adapter2 = new SqlDataAdapter();
    adapter2.SelectCommand = command2;
    adapter2.Fill(ds, "articles_Details"); //articles becomes the Tables[1] in the ds DataSet


// LINQ code to query ds DataSet:

var query = from artic in ds.Tables["articles"].AsEnumerable()
            join artic_det in ds.Tables["articles_Details"].AsEnumerable()
            on artic.Field<int>("id_art") equals artic_det.Field<int>("id_art")

            select new
            {
                id_art_in_articles = artic.Field<int>("articles"),
                descr_article = artic.Field.Field<string>("description"),
                id_art_in_articles_Details = artic_det.Field<int>("articles_Details")
            };


     closeConnections(); // close connections for SqlCommands I'm using in this piece of code        

}

然后在控制台上输出每条记录,我可以验证这实际上是两个表之间的 INNER JOIN。

【讨论】:

    猜你喜欢
    • 2018-11-13
    • 2023-03-30
    • 1970-01-01
    • 2019-08-05
    • 2021-01-16
    • 1970-01-01
    • 2018-06-07
    • 2012-10-23
    相关资源
    最近更新 更多