C#代码从多个表中检索数据 [英] C# code to retrieve data from multiple tables

查看:171
本文介绍了C#代码从多个表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信息存储在数据库的差异表中。例如DB名称说DB1。它有诸如任务,故事,人物等的表格。我想检查条件,好像task.acceptor_id和story.owner_id = person.userId,然后任务和故事的名称应该显示在特定用户''页面。

我还想在项目网站中显示用户/开发人员的任务/故事的详细信息,检查用户/开发人员只被分配到任何任务/故事的条件。项目。或者它不应该在他/她的页面中显示任何任务/故事相关信息。

应该在C#

完成。请帮我完成它。

提前致谢...

The informations are stored in diff tables of a database. For instance DB name say DB1. It has tables such as task,story,person,... I want to check the condition as if task.acceptor_id and story.owner_id=person.userId, then name of the task and story should be displayed in the particular user''s page.
I also want to display the details of the tasks/story for a user/dev in a project website checking the condition that the user/developer is only assigned to any task/story of the project. Or it should not be display any tasks/story related information in his/her page.
should be done in C#
Please help me to finish it.
Thanks in advance...

推荐答案

尝试使用SQL JOIN:

Try using an SQL JOIN:
SELECT t.Name, s.Name FROM person p
JOIN story s ON p.userId=s.owner_id
JOIN task t ON p.userId=t.acceptor_id

您所要做的就是将其打包成C#代码:

All you have to do is package it in C# code:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT t.Name AS task, s.Name AS story FROM person p JOIN story s ON p.userId=s.owner_id JOIN task t ON p.userId=t.acceptor_id", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                string task = (string) reader["task"];
                string story = (string) reader["story"];
                Console.WriteLine("{0}, {1}", task, story);
                }
            }
        }
    }


这篇关于C#代码从多个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆