~ubuntu-branches/ubuntu/quantal/psqlodbc/quantal

« back to all changes in this revision

Viewing changes to docs/howto-csharp.html

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2007-03-05 22:28:19 UTC
  • mfrom: (3.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20070305222819-95d0rzmt2ah6dwwc
Tags: 1:08.01.0200-2.1
* Non-maintainer upload.
* High-urgency upload for RC bugfix.
* Fix the signature of SQLGetData on 64-bit architectures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
2
<html>
 
3
  <head>
 
4
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
 
5
    <title>psqlODBC HOWTO - C#</title>
 
6
  </HEAD>
 
7
 
 
8
  <body bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#a00000" alink="#0000ff">
 
9
  
 
10
<h1>psqlODBC HOWTO - C#</h1>
 
11
 
 
12
<p>
 
13
 
 
14
<i>
 
15
Author: Dave Page (dpage@postgresql.org)<br>
 
16
Release Date: 12 April 2002<br>
 
17
Description: Example based Mini-Howto on Accessing PostgreSQL from C#
 
18
</i>
 
19
<br><br>
 
20
This document provides some sample code to get you started with C# & PostgreSQL.
 
21
<br><br>
 
22
Requirements to get the code to work:
 
23
 
 
24
<br>
 
25
<ul>
 
26
<li>A C# Compiler.</li>
 
27
<li>The Microsoft .NET Framework.</li>
 
28
<li>The Microsoft ODBC .NET Data Provider.</li>
 
29
<li>A PostgreSQL datasource.</li>
 
30
</ul>
 
31
 
 
32
The example code shown below may need some modification to make it actually work in your environment. 
 
33
There is one table used in the example:
 
34
 
 
35
<br>
 
36
<blockquote>
 
37
<pre>
 
38
CREATE TABLE vbtest(
 
39
&nbsp;&nbsp;  id serial,
 
40
&nbsp;&nbsp;  data text,
 
41
&nbsp;&nbsp;  accessed timestamp
 
42
);
 
43
INSERT INTO csharptest(data, accessed) VALUES('Rows: 1', now());
 
44
INSERT INTO csharptest(data, accessed) VALUES('Rows: 2', now());
 
45
INSERT INTO csharptest(data, accessed) VALUES('Rows: 3', now());
 
46
</pre>
 
47
</blockquote> 
 
48
 
 
49
<h2>Code</h2>
 
50
 
 
51
<blockquote>
 
52
<pre>
 
53
 
 
54
using System;
 
55
using System.Data;
 
56
using Microsoft.Data.Odbc;
 
57
 
 
58
 
 
59
class psqlODBC_Howto
 
60
{
 
61
 
 
62
  [STAThread]
 
63
  static void Main(string[] args)
 
64
  {
 
65
 
 
66
    // Setup a connection string
 
67
    string szConnect = "DSN=dsnname;" +
 
68
                       "UID=postgres;" +
 
69
                       "PWD=********";
 
70
 
 
71
    // Attempt to open a connection
 
72
    OdbcConnection cnDB = new OdbcConnection(szConnect);
 
73
     
 
74
    // The following code demonstrates how to catch & report an ODBC exception.
 
75
    // To keep things simple, this is the only exception handling in this example.
 
76
    // Note: The ODBC data provider requests ODBC3 from the driver. At the time of
 
77
    //       writing, the psqlODBC driver only supports ODBC2.5 - this will cause
 
78
    //       an additional error, but will *not* throw an exception.
 
79
    try 
 
80
    {
 
81
      cnDB.Open();
 
82
    } 
 
83
    catch (OdbcException ex) 
 
84
    {
 
85
      Console.WriteLine (ex.Message + "\n\n" + "StackTrace: \n\n" + ex.StackTrace);
 
86
      // Pause for the user to read the screen.
 
87
      Console.WriteLine("\nPress <RETURN> to continue...");
 
88
      Console.Read();
 
89
      return;
 
90
    }
 
91
    
 
92
    // Create a dataset
 
93
    DataSet dsDB = new DataSet(); 
 
94
    OdbcDataAdapter adDB = new OdbcDataAdapter();
 
95
    OdbcCommandBuilder cbDB = new OdbcCommandBuilder(adDB);
 
96
    adDB.SelectCommand = new OdbcCommand(
 
97
                             "SELECT id, data, accessed FROM csharptest", 
 
98
                             cnDB);
 
99
    adDB.Fill(dsDB);
 
100
 
 
101
    // Display the record count
 
102
    Console.WriteLine("Table 'csharptest' contains {0} rows.\n", 
 
103
                      dsDB.Tables[0].Rows.Count);
 
104
    
 
105
    // List the columns (using a foreach loop)
 
106
    Console.WriteLine("Columns\n=======\n");
 
107
    
 
108
    foreach(DataColumn dcDB in dsDB.Tables[0].Columns)
 
109
      Console.WriteLine("{0} ({1})", dcDB.ColumnName, dcDB.DataType);
 
110
    Console.WriteLine("\n");
 
111
 
 
112
    // Iterate through the rows and display the data in the table (using a for loop).
 
113
    // Display the data column last for readability.
 
114
    Console.WriteLine("Data\n====\n");
 
115
    for(int i=0;i<dsDB.Tables[0].Rows.Count;i++){
 
116
      Console.WriteLine("id: {0}, accessed: {1}, data: {2}", 
 
117
                        dsDB.Tables[0].Rows[i]["id"], 
 
118
                        dsDB.Tables[0].Rows[i]["accessed"], 
 
119
                        dsDB.Tables[0].Rows[i]["data"]);
 
120
    }
 
121
    
 
122
    // Add a new row to the table using the dataset
 
123
    // Create a new row on the existing dataset, then set the values and add the row
 
124
    Console.WriteLine("\nInserting a new row...");
 
125
    DataRow rwDB = dsDB.Tables[0].NewRow();
 
126
    int iRows = dsDB.Tables[0].Rows.Count + 1;
 
127
    rwDB["data"] = "Rows: " + iRows.ToString();
 
128
    rwDB["accessed"] = System.DateTime.Now;
 
129
    dsDB.Tables[0].Rows.Add(rwDB);
 
130
    adDB.Update(dsDB);
 
131
    
 
132
    // Delete a row from the table using a direct SQL query.
 
133
    // This method can also be used for direct INSERTs UPDATEs, CREATEs DROPs and more.
 
134
    Console.WriteLine("\nDeleting the row with the lowest ID...");
 
135
    OdbcCommand cmDB = new OdbcCommand(
 
136
                           "DELETE FROM csharptest WHERE id = (SELECT min(id) FROM csharptest)", 
 
137
                           cnDB);
 
138
    cmDB.ExecuteNonQuery();
 
139
 
 
140
    // Execute a scalar query
 
141
    cmDB = new OdbcCommand("SELECT max(id) FROM csharptest", cnDB);
 
142
    string szMax = cmDB.ExecuteScalar().ToString();    
 
143
    Console.WriteLine("\nThe maximum value in the id column is now: {0}", szMax);
 
144
    
 
145
    // Pause for the user to read the screen.
 
146
    Console.WriteLine("\nPress <RETURN> to continue...");
 
147
    Console.Read();
 
148
  }
 
149
}
 
150
 
 
151
</pre>
 
152
</blockquote>
 
153
</p>
 
154
 
 
155
</body>
 
156
</html>
 
 
b'\\ No newline at end of file'