9. Understanding .NET Data Provider Connectivity : .NET Data Provider Classes : Sample Program Constructed with .NET Data Provider : .NET 2.0 Programming Model
 
Share this page                  
.NET 2.0 Programming Model
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.IO;
using Ingres.Client;
 
class App
{
static public void Main()
{
ConnectionStringSettingsCollection connectionSettings =
        ConfigurationManager.ConnectionStrings;
if (connectionSettings.Count == 0)
        throw new InvalidOperationException(
         "No connection information specified in application configuration file.");
ConnectionStringSettings connectionSetting = connectionSettings[0];
 
string invariantName      = connectionSetting.ProviderName;
string myConnectionString = connectionSetting.ConnectionString;
 
DbProviderFactory factory = DbProviderFactories.GetFactory(invariantName);
 
DbConnection conn =
         factory.CreateConnection();
conn.ConnectionString = myConnectionString;
 
conn.Open();   // open the Ingres connection
 
string cmdtext =
        "select table_owner, table_name, " +
        " create_date from iitables " +
        " where table_type in ('T','V') and " +
        " table_name not like 'ii%' and" +
        " table_name not like 'II%'";
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = cmdtext;
 
//          read the data using the DataReader method
DbDataReader   datareader = cmd.ExecuteReader();
 
//          write header labels
Console.WriteLine(datareader.GetName(0).PadRight(18) +
datareader.GetName(1).PadRight(34) +
datareader.GetName(2).PadRight(34));
int i = 0;
while (i++ < 10 && datareader.Read())
// read and write out a few data rows
{     // write out the three columns to the console
        Console.WriteLine(
        datareader.GetString(0).Substring(0,16).PadRight(18) +
        datareader.GetString(1).PadRight(34) +
                datareader.GetString(2));
}
datareader.Close();
 
DataSet  ds  = new DataSet("my_list_of_tables");
//          read the data using the DataAdapter method
DbDataAdapter adapter = factory.CreateDataAdapter();
DbCommand adapterCmd = conn.CreateCommand();
adapterCmd.CommandText = cmdtext;
adapter.SelectCommand = adapterCmd;
adapter.Fill(ds);  // fill the dataset
 
//          write the dataset to an XML file
ds.WriteXml("c:/temp/temp.xml");
 
conn.Close();   // close the connection
}  // end Main()
}  // end class App