head	1.1;
access;
symbols
	REL-09_01_0200:1.1
	REL-09_01_0100:1.1
	REL-09_00_0310:1.1
	REL-09_00_0300:1.1
	REL-09_00_0200:1.1
	REL-09_00_0101:1.1
	REL-09_00_0100:1.1
	REL-08_04_0200:1.1
	REL-08_04_0100:1.1
	REL-08_03_0400:1.1
	REL-08_03_0300:1.1
	REL-08_03_0200:1.1
	REL-08_03_0100:1.1
	REL-08_02_0500:1.1
	REL-08_02_0400:1.1
	REL-08_02_0300:1.1
	REL-08_02_0200:1.1
	REL-08_02_0100:1.1
	REL8_1_STABLE:1.1.0.2
	REL-08_01_0200:1.1
	REL-08_01_0102:1.1
	REL-08_01_0101:1.1
	REL-08_01_0100:1.1
	REL-08_01_0006:1.1
	REL-08_01_0005:1.1
	REL-08_01_0004:1.1;
locks; strict;
comment	@# @;


1.1
date	2005.08.27.19.44.47;	author dpage;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Move docs into the source tree.
@
text
@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>psqlODBC HOWTO - C#</title>
  </HEAD>

  <body bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#a00000" alink="#0000ff">
  
<h1>psqlODBC HOWTO - C#</h1>

<p>

<i>
Author: Dave Page (dpage@@postgresql.org)<br>
Release Date: 12 April 2002<br>
Description: Example based Mini-Howto on Accessing PostgreSQL from C#
</i>
<br><br>
This document provides some sample code to get you started with C# & PostgreSQL.
<br><br>
Requirements to get the code to work:

<br>
<ul>
<li>A C# Compiler.</li>
<li>The Microsoft .NET Framework.</li>
<li>The Microsoft ODBC .NET Data Provider.</li>
<li>A PostgreSQL datasource.</li>
</ul>

The example code shown below may need some modification to make it actually work in your environment. 
There is one table used in the example:

<br>
<blockquote>
<pre>
CREATE TABLE vbtest(
&nbsp;&nbsp;  id serial,
&nbsp;&nbsp;  data text,
&nbsp;&nbsp;  accessed timestamp
);
INSERT INTO csharptest(data, accessed) VALUES('Rows: 1', now());
INSERT INTO csharptest(data, accessed) VALUES('Rows: 2', now());
INSERT INTO csharptest(data, accessed) VALUES('Rows: 3', now());
</pre>
</blockquote> 

<h2>Code</h2>

<blockquote>
<pre>

using System;
using System.Data;
using Microsoft.Data.Odbc;
 

class psqlODBC_Howto
{

  [STAThread]
  static void Main(string[] args)
  {

    // Setup a connection string
    string szConnect = "DSN=dsnname;" +
                       "UID=postgres;" +
                       "PWD=********";

    // Attempt to open a connection
    OdbcConnection cnDB = new OdbcConnection(szConnect);
     
    // The following code demonstrates how to catch & report an ODBC exception.
    // To keep things simple, this is the only exception handling in this example.
    // Note: The ODBC data provider requests ODBC3 from the driver. At the time of
    //       writing, the psqlODBC driver only supports ODBC2.5 - this will cause
    //       an additional error, but will *not* throw an exception.
    try 
    {
      cnDB.Open();
    } 
    catch (OdbcException ex) 
    {
      Console.WriteLine (ex.Message + "\n\n" + "StackTrace: \n\n" + ex.StackTrace);
      // Pause for the user to read the screen.
      Console.WriteLine("\nPress <RETURN> to continue...");
      Console.Read();
      return;
    }
    
    // Create a dataset
    DataSet dsDB = new DataSet(); 
    OdbcDataAdapter adDB = new OdbcDataAdapter();
    OdbcCommandBuilder cbDB = new OdbcCommandBuilder(adDB);
    adDB.SelectCommand = new OdbcCommand(
                             "SELECT id, data, accessed FROM csharptest", 
                             cnDB);
    adDB.Fill(dsDB);

    // Display the record count
    Console.WriteLine("Table 'csharptest' contains {0} rows.\n", 
                      dsDB.Tables[0].Rows.Count);
    
    // List the columns (using a foreach loop)
    Console.WriteLine("Columns\n=======\n");
    
    foreach(DataColumn dcDB in dsDB.Tables[0].Columns)
      Console.WriteLine("{0} ({1})", dcDB.ColumnName, dcDB.DataType);
    Console.WriteLine("\n");

    // Iterate through the rows and display the data in the table (using a for loop).
    // Display the data column last for readability.
    Console.WriteLine("Data\n====\n");
    for(int i=0;i<dsDB.Tables[0].Rows.Count;i++){
      Console.WriteLine("id: {0}, accessed: {1}, data: {2}", 
                        dsDB.Tables[0].Rows[i]["id"], 
                        dsDB.Tables[0].Rows[i]["accessed"], 
                        dsDB.Tables[0].Rows[i]["data"]);
    }
    
    // Add a new row to the table using the dataset
    // Create a new row on the existing dataset, then set the values and add the row
    Console.WriteLine("\nInserting a new row...");
    DataRow rwDB = dsDB.Tables[0].NewRow();
    int iRows = dsDB.Tables[0].Rows.Count + 1;
    rwDB["data"] = "Rows: " + iRows.ToString();
    rwDB["accessed"] = System.DateTime.Now;
    dsDB.Tables[0].Rows.Add(rwDB);
    adDB.Update(dsDB);
    
    // Delete a row from the table using a direct SQL query.
    // This method can also be used for direct INSERTs UPDATEs, CREATEs DROPs and more.
    Console.WriteLine("\nDeleting the row with the lowest ID...");
    OdbcCommand cmDB = new OdbcCommand(
                           "DELETE FROM csharptest WHERE id = (SELECT min(id) FROM csharptest)", 
                           cnDB);
    cmDB.ExecuteNonQuery();

    // Execute a scalar query
    cmDB = new OdbcCommand("SELECT max(id) FROM csharptest", cnDB);
    string szMax = cmDB.ExecuteScalar().ToString();    
    Console.WriteLine("\nThe maximum value in the id column is now: {0}", szMax);
    
    // Pause for the user to read the screen.
    Console.WriteLine("\nPress <RETURN> to continue...");
    Console.Read();
  }
}

</pre>
</blockquote>
</p>

</body>
</html>@
