Print DataSet out

image_pdfimage_print

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.
“, ds.DataSetName);
foreach (DataTable dt in ds.Tables) {
Console.WriteLine(“{0} Table.
“, dt.TableName);
for (int curCol = 0; curCol < dt.Columns.Count; curCol++) { Console.Write(dt.Columns[curCol].ColumnName.Trim() + " "); } 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() + " "); } Console.WriteLine(); } } } } [/csharp]