What DataSet does this code use? The documentation for SqlDataAdapter.Fill(DataTable) makes them sound mandatory

175 Views Asked by At

The following code is adapted from Microsoft's code here. It is therefore obviously correct.

using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        ReturnIdentity(connectionString);
    }

    private static void ReturnIdentity(string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            // Create a SqlDataAdapter based on a SELECT query.
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM dbo.Categories", connection);

            // Create a DataTable and fill it.
            DataTable categories = new DataTable();
            adapter.Fill(categories);
        }
    }

    static private string GetConnectionString()
    {
       return "Your Connection String";
    }  
}

Despite this obviously correct code containing no DataSet that I can find, the documentation for the Fill method called with adapter.Fill(categories) above makes repeated mentions of DataSets, as if they're somehow relevant/mandatory to using the Fill(DataTable) method of the SqlDataAdapter class.

This gives me my question: Where in the above code does any DataSet appear? I can't think of any reading of the documentation for SqlDataAdapter.Fill(DataTable) that doesn't make it sound like you absolutely need one.

3

There are 3 best solutions below

7
Tom On

The SqlDataAdapter class has multiple overloaded versions of Fill that accepts different arguments, including DataSet and DataTable instances. See the documentation for a complete list.

2
bdcoder On

Perhaps the following will help:

  • A DataSet is made up of a collection of tables, relationships, and constraints. In ADO.NET, DataTable objects are used to represent the tables in a DataSet. A DataTable represents one table of in-memory relational data; the data is local to the .NET-based application in which it resides, but can be populated from a data source such as Microsoft SQL Server using a DataAdapter For more information, see Populating a DataSet from a DataAdapter.

  • The overload of Fill that takes DataTable as a parameter only obtains the first result. Use an overload of Fill that takes DataSet as a parameter to obtain multiple results.

So, looking at the code above, the overload is using a DataTable as a parameter, therefore (as per the docs), the the first result in the DataSet will be used.

I think the reference to a DataSet has to do with the SqlDataAdapter - which uses DataSets "under the hood" and the Fill method can accept a DataTable parameter, which is then filled from an underlying DataSet.

1
Joel Coehoorn On

You need to read this in the Remarks section:

The overload of Fill that takes DataTable as a parameter only obtains the first result. Use an overload of Fill that takes DataSet as a parameter to obtain multiple results.

So we see a DataSet is only required if a query batch could have multiple result sets.