howto-csharp.html
来自「postgresql-odbc,跨平台应用」· HTML 代码 · 共 156 行
HTML
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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?