简体   繁体   English

从SQLDataReader使用多个结果集

[英]Using multiple result sets from a SQLDataReader

I currently working on a school project and was hoping that someone could point me to an on line tutorial for the following. 我目前正在研究一个学校项目,希望有人可以指出以下内容的在线教程。 This is a homework assignment so I am not looking for someone to give me the answer but I would like to find a tutorial that is similar to my intended solution. 这是一项家庭作业,所以我不是在找别人给我答案,而是想找到与我所打算的解决方案相似的教程。 I have been googling SQLDataReader and Linq and have not been able to find a solution similar to what I am pursuing. 我一直在使用SQLDataReader和Linq进行搜索,但找不到与我追求的解决方案类似的解决方案。

I'm connecting my C# client project to an SQL database. 我正在将C#客户端项目连接到SQL数据库。 This database contains a table for Dogs and a table for BreedOfDog to account for the fact that dogs can be of mixed breeds. 该数据库包含一个用于狗的表和一个用于BreedOfDog的表,以说明狗可能属于混合犬种这一事实。 BreedOfDog has two values, DogId and BreedId, that connects the Dog table to the Breed table. BreedOfDog具有两个值DogId和BreedId,它们将Dog表连接到Breed表。 On my client side application there is a container that presents the information for the dog. 在我的客户端应用程序上,有一个容器,用于显示狗的信息。 There is also a Dog class that contains a list of breed objects. 还有一个Dog类,其中包含品种对象列表。 The list of breed objects will be used to populate a listbox with the dog's breeds. 品种对象列表将用于用狗的品种填充列表框。 This is what I intend to do in pseudocode: 这是我打算用伪代码执行的操作:

Create a SQL query that will allow for multiple result sets. 创建一个允许多个结果集的SQL查询。 I will be using stored procedures from the SQL database 我将使用SQL数据库中的存储过程

Open connection to the SQL database 打开与SQL数据库的连接

Perform the first query that will obtain a list of Dog objects 执行第一个查询,将获得Dog对象的列表

Perform the second query. 执行第二个查询。 This query will pull the DogId for each dog in the first query, execute the query on BreedOfDog, create a list of Breed objects and add this to the Dog object. 此查询将在第一个查询中拉出每条狗的DogId,在BreedOfDog上执行查询,创建Breed对象的列表并将其添加到Dog对象。 This will be executed for each Dog in the list of Dogs. 将对“狗”列表中的每个狗执行此操作。

Close connection 紧密连接

Is there a good turtorial that you can point me to? 您可以指出我的观点吗?

    List<DogClass> Dogs = new List<DogClass>();
    string SQL = "select DogId from Dog";
    SqlCommand Command = new SqlCommand(SQL, Con);
    SqlDataReader Reader = Command.ExecuteReader();
    while (Reader.Read())
    {
        DogClass Dog = new DogClass(Reader, Con);
        Dogs.Add(Dog);
    }
    Reader.Close();

using System;
using System.Collections.Generic;
using System.Data.SqlClient;

class DogClass
{
    string DogId;
    List<BreedClass> Breeds = new List<BreedClass>();

    internal DogClass(SqlDataReader Reader, SqlConnection Con)
    {
        DogId = Convert.ToString(Reader.GetValue(Reader.GetOrdinal("DogId"))).Trim();
        string SQL = "select BreedOfDog from Breeds where DogID = '" + DogId + "'";
        SqlCommand Command = new SqlCommand(SQL, Con);
        SqlDataReader Reader2 = Command.ExecuteReader();
        while (Reader2.Read())
        {
            BreedClass Breed = new BreedClass(Reader);
            Breeds.Add(Breed);
        }
        Reader2.Close();
    }
}

using System;
using System.Data.SqlClient;

class BreedClass
{
    internal string Breed;

    internal BreedClass(SqlDataReader Reader)
    {
        Breed = Convert.ToString(Reader.GetValue(Reader.GetOrdinal("BreedOfDog"))).Trim();
    }
}

I remember doing this same problem in programmers school (college)! 我记得在程序员学校(学院)也遇到过同样的问题! You'll love linq to sql. 您会喜欢linq to sql的。 What you need to do is: 您需要做的是:

  1. Right click the folder where you want to put your linq data class. 右键单击要放置linq数据类的文件夹。 Click add new item -> LINQ to SQL classes. 单击添加新项-> LINQ to SQL类。
  2. Then, open your server explorer and drag and drop the tables onto the LINQ to SQL class designer. 然后,打开服务器资源管理器并将表拖放到LINQ to SQL类设计器上。
  3. Then, go into your .cs page where you want to use the class and instantiate a new object for your database class like DataClasses1DataContext db = new DataClasses1DataContext(); 然后,进入要使用该类的.cs页面,并为数据库类实例化一个新对象,例如DataClasses1DataContext db = new DataClasses1DataContext(); .
  4. Then you can manipulate your database via LINQ to SQL statements as easy as IQueryable<BREED> breeds = db.BREEDs.Take(50); 然后,您可以通过LINQ to SQL语句操作数据库,就像IQueryable<BREED> breeds = db.BREEDs.Take(50);一样简单IQueryable<BREED> breeds = db.BREEDs.Take(50); . See this article for info on how to use LINQ to read/write/delete/etc. 有关如何使用LINQ读取/写入/删除/等的信息,请参阅本文。 http://msdn.microsoft.com/en-us/library/bb882643.aspx http://msdn.microsoft.com/en-us/library/bb882643.aspx

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

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