Print DataSet out : DataSet : Database C# Source Code


Custom Search

C# Source Code » Database » DataSet »

 

Print DataSet out








    


using System;
using System.Collections.Generic;
using System.Text;

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

class Program {
    static void Main(string[] args) {
        string cnStr = "uid=sa;pwd=;Initial Catalog=yourDatabase;Data Source=(local)";
        DataSet myDS = new DataSet("Cars");
        SqlDataAdapter dAdapt = new SqlDataAdapter("Select * From Inventory", cnStr);
        DataTableMapping custMap = dAdapt.TableMappings.Add("Inventory", "Current Inventory");
        custMap.ColumnMappings.Add("CarID", "Car ID");
        custMap.ColumnMappings.Add("PetName", "Name of Car");

        try {
            dAdapt.Fill(myDS, "Inventory");
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
        PrintDataSet(myDS);
    }
    static void PrintDataSet(DataSet ds) {
        Console.WriteLine("Tables in '{0}' DataSet.\n", ds.DataSetName);
        foreach (DataTable dt in ds.Tables) {
            Console.WriteLine("{0} Table.\n", dt.TableName);
            for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
                Console.Write(dt.Columns[curCol].ColumnName.Trim() + "\t");
            }
            for (int curRow = 0; curRow < dt.Rows.Count; curRow++) {
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
                    Console.Write(dt.Rows[curRow][curCol].ToString().Trim() + "\t");
                }
                Console.WriteLine();
            }
        }
    }

}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» DataSet