using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;
using System.Linq;using System.Text;
using System.Windows.Forms;using Npgsql;
namespace org.lreis.grid
{
public partial class Form2 : Form {
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
private string sql = "SELECT asewkt(the_geom) FROM florida_polygon";
public Form2() {
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
try {
// PostgeSQL-style connection string
string connstring = String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database={4};",
tbHost.Text, tbPort.Text, tbUser.Text,
tbPass.Text, tbDataBaseName.Text);
// Making connection with Npgsql provider
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
// quite complex sql statement
// data adapter making request from our connection
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
// i always reset DataSet before i do
// something with it.... i don't know why :-)
ds.Reset();
// filling DataSet with result from NpgsqlDataAdapter
da.Fill(ds);
// since it C# DataSet can handle multiple tables, we will select first
dt = ds.Tables[0];
// connect grid to DataTable
dataGridView1.DataSource = dt;
// since we only showing the result we don't need connection anymore
conn.Close();
}
catch (Exception msg)
{ // something went wrong, and you wanna know why
MessageBox.Show(msg.ToString());
throw; } }
}}
"SELECT asewkt(the_geom) FROM florida_polygon";查询几何数据。
重点是引用Npgsql.dll,下载地址http://pgfoundry.org/frs/?group_id=1000140。