Create and fill DataSet from the Northwind database

Source: Internet
Author: User
Using System;
Using System. Data;
Using System. Data. SqlClient;

Namespace Microsoft. AdoNet. DataSetDemo
{
Class NorthwindDataSet
{
Static void Main ()
{
String connectionString = GetConnectionString ();
ConnectToData (connectionString );
}

Private static void ConnectToData (string connectionString)
{
// Create a SqlConnection to the Northwind database.
Using (SqlConnection connection =
New SqlConnection (connectionString ))
{
// Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter ();

// A table mapping names the DataTable.
Adapter. TableMappings. Add ("Table", "Suppliers ");

// Open the connection.
Connection. Open ();
Console. WriteLine ("The SqlConnection is open .");

// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand (
"SELECT SupplierID, CompanyName FROM dbo. Suppliers ;",
Connection );
Command. CommandType = CommandType. Text;

// Set the SqlDataAdapter's SelectCommand.
Adapter. SelectCommand = command;

// Fill the DataSet.
DataSet dataSet = new DataSet ("Suppliers ");
Adapter. Fill (dataSet );

// Create a second Adapter and Command to get
// The Products table, a child table of Suppliers.
SqlDataAdapter productsAdapter = new SqlDataAdapter ();
ProductsAdapter. TableMappings. Add ("Table", "Products ");

SqlCommand productsCommand = new SqlCommand (
"SELECT ProductID, SupplierID FROM dbo. Products ;",
Connection );
ProductsAdapter. SelectCommand = productsCommand;

// Fill the DataSet.
ProductsAdapter. Fill (dataSet );

// Close the connection.
Connection. Close ();
Console. WriteLine ("The SqlConnection is closed .");

// Create a DataRelation to link the two tables
// Based on the SupplierID.
DataColumn parentColumn =
DataSet. Tables ["Suppliers"]. Columns ["SupplierID"];
DataColumn childColumn =
DataSet. Tables ["Products"]. Columns ["SupplierID"];
DataRelation relation =
New System. Data. DataRelation ("SuppliersProducts ",
ParentColumn, childColumn );
DataSet. Relations. Add (relation );
Console. WriteLine (
"The {0} DataRelation has been created .",
Relation. RelationName );
}
}

Static private string GetConnectionString ()
{
// To avoid storing the connection string in your code,
// You can retrieve it from a configuration file.
Return "Data Source = (local); Initial Catalog = Northwind ;"
+ "Integrated Security = SSPI ";
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.