/usr/share/doc/odbc-postgresql/docs/howto-csharp.html is in odbc-postgresql 1:09.00.0310-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | <!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(
id serial,
data text,
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>
|