user1355240
user1355240

Reputation:

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.

I'm connecting my C# client project to an SQL database. 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 has two values, DogId and BreedId, that connects the Dog table to the Breed table. 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. 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. I will be using stored procedures from the SQL database

Open connection to the SQL database

Perform the first query that will obtain a list of Dog objects

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. 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?

Upvotes: 1

Views: 929

Answers (2)

user1477388
user1477388

Reputation: 21430

I remember doing this same problem in programmers school (college)! You'll love linq to sql. What you need to do is:

  1. Right click the folder where you want to put your linq data class. Click add new item -> LINQ to SQL classes.
  2. Then, open your server explorer and drag and drop the tables onto the LINQ to SQL class designer.
  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();.
  4. Then you can manipulate your database via LINQ to SQL statements as easy as IQueryable<BREED> breeds = db.BREEDs.Take(50);. See this article for info on how to use LINQ to read/write/delete/etc. http://msdn.microsoft.com/en-us/library/bb882643.aspx

Upvotes: 0

Michael Ross
Michael Ross

Reputation: 570

    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();
    }
}

Upvotes: 1

Related Questions