/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.Data;
using System.Data.OleDb;
namespace Client.Chapter_13___ADO.NET
{
public class UsingMultiTabledDatasets
{
static void Main(string[] args)
{
OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsft.Jet.OLEDB.4.0; Data Source = c:MyAccessDB.mdb");
OleDbDataAdapter MyAdapter = new OleDbDataAdapter("SELECT Column1, Column2, Column3 FROM MyTable", MyConnection);
DataSet MyDataSet = new DataSet();
MyAdapter.Fill(MyDataSet, "MyTable");
foreach (DataTable MyTable in MyDataSet.Tables)
{
foreach (DataColumn MyColumn in MyTable.Columns)
{
foreach (DataRow MyRow in MyTable.Rows)
{
}
}
}
}
}
}
Month: February 2011
Using Datasets
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.Data;
using System.Data.SqlClient;
namespace Client.Chapter_13___ADO.NET
{
public class UsingDatasets
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
SqlDataAdapter MyAdapter = new SqlDataAdapter("SELECT * FROM CaseInfo", MyConnection);
DataSet MyDataSet = new DataSet();
MyAdapter.Fill(MyDataSet, "MyTable");
MyDataSet.WriteXml(@"c:MyDatSet.xml");
}
}
}
Populate a DataSet object with a range of rows from a SELECT statement
using System;
using System.Data;
using System.Data.SqlClient;
class PopulateDataSetUsingRange
{
public static void Main()
{
SqlConnection mySqlConnection =new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 5 ID, FirstName, LastName " +
"FROM Employee " +
"ORDER BY ID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
Console.WriteLine("Retrieving rows from the Employee table");
int numberOfRows = mySqlDataAdapter.Fill(myDataSet, 1, 3, "Employee");
Console.WriteLine("numberOfRows = " + numberOfRows);
mySqlConnection.Close();
DataTable myDataTable = myDataSet.Tables["Employee"];
foreach (DataRow myDataRow in myDataTable.Rows)
{
Console.WriteLine("ID = " + myDataRow["ID"]);
Console.WriteLine("FirstName = " + myDataRow["FirstName"]);
Console.WriteLine("LastName = " + myDataRow["LastName"]);
}
}
}
Open the XML file and read into a DataSet
using System;
using System.IO;
using System.Data;
public class MainClass {
static void Main(string[] args) {
if (args.Length != 1)
return;
FileStream fs = new FileStream(args[0], FileMode.Open);
DataSet ds = new DataSet();
ds.ReadXml(fs);
// Use a DataTable to display the members.
DataTable mt = ds.Tables[“member”];
for (int row = 0; row < mt.Rows.Count; row++) {
for (int col = 0; col < mt.Columns.Count - 1; col++) {
Console.WriteLine("{0,-10}{1}",
mt.Columns[col].Caption,
mt.Rows[row][col].ToString().Trim());
}
Console.WriteLine();
}
fs.Close();
}
}
[/csharp]
ReadXml
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
DataSet thisDataSet = new DataSet();
thisDataSet.ReadXml("nwinddata.xml");
foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {
Console.WriteLine("Customer ID: " + custRow["CustomerID"] +
" Name: " + custRow["CompanyName"]);
}
Console.WriteLine("Table created by ReadXml is called {0}",
thisDataSet.Tables[0].TableName);
}
}
DataSet Read
using System;
using System.Data; // Use ADO.NET namespace
using System.Data.SqlClient; // Use SQL Server data provider namespace
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
SqlConnection thisConnection = new SqlConnection(
@"Server=(local)sqlexpress;Integrated Security=True;" +
"Database=northwind");
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"SELECT CustomerID, ContactName FROM Customers", thisConnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Customers");
foreach (DataRow theRow in thisDataSet.Tables["Customers"].Rows) {
Console.WriteLine(theRow["CustomerID"] + " " +
theRow["ContactName"]);
}
thisConnection.Close();
}
}
For each row in DataSet, reference the column data by column name
using System;
using System.Data;
using System.Data.SqlClient;
class Class1{
static void Main(string[] args){
SqlConnection thisConnection = new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT ID, FirstName FROM Employee", thisConnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Employee");
foreach (DataRow theRow in thisDataSet.Tables["Employee"].Rows)
{
Console.WriteLine(theRow["ID"] + " " + theRow["FirstName"]);
}
}
}

