1 Replies - 676 Views - Last Post: 24 August 2017 - 05:16 PM Rate Topic: -----

#1 Milan22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-August 17

DataSet to C#console

Posted 24 August 2017 - 02:27 AM

I have some code which should list ProductName to corresponding CatecoryName of NORTHWIND db, by using DataSet
i have so called problem on the end ,after adding relationships to DataSets how to force console to list product with corresponding catergory names, thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using.System.Data.Common;

namespace consoleAsgDatAccess
{
    class Program
    {
        static void Main(string[] args)
        {

            // Create the Connection, DataAdapter, and DataSet.

            string connectionString = "Data Source=localhost;Initial Catalog=NORTHWND;" + "Integrated Security=true";
            SqlConnection con = new SqlConnection(connectionString);
            string sqlCat = "SELECT CategoryID, CategoryName FROM Categories";
            string sqlProd = "SELECT ProductName, CategoryID FROM Products";

            SqlDataAdapter da = new SqlDataAdapter(sqlCat, con);
            DataSet ds = new DataSet();

            try
            {
                con.Open();

                //Filling tge DataSet with the Categories table.
                da.Fill(ds, "Categories");

                //Changing the command text and retrieve the Products table.
                // Oprional using another DataAdapterr object for this task.
                da.SelectCommand.CommandText = sqlProd;
                da.Fill(ds, "Products");
            }
            finally
            {
                con.Close();
            }

            //Defining the relationship beween Categories and Products.
            DataRelation relat = new DataRelation("CatProds",
            ds.Tables["Categories"].Columns["CategoryID"],
            ds.Tables["Products"].Columns["CategoryID"]);

            //Adding the relationship to the DataSet.
            ds.Relations.Add(relat);





Is This A Good Question/Topic? 0
  • +

Replies To: DataSet to C#console

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: DataSet to C#console

Posted 24 August 2017 - 05:16 PM

Did you bother to read the documentation? ChildViews and Relations It even has sample code to do what you want.

In general, though, most people don't use the DataSet to do that work since that requires the server to send all the data to the client and for the client to hold all the data in memory. Instead most people let the server do all the work by using the SQL JOIN keyword when selecting data.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1