[Mono-docs-list] ASP.NET example primed for mono-project postgreSQL page

mono.docs.list at jumpstation.co.uk mono.docs.list at jumpstation.co.uk
Wed May 16 11:44:13 EDT 2007


hi,

I saw the C# example on http://www.mono-project.com/PostgreSQL using Npgsql 
and found it very useful in testing installation.

I thought it would benifit others if I refactored it into ASP.NET/C# as this 
was why I setup postgreSQL.
So I was a bit surprised when found I could not edit the page or even register 
an account.

Code follows as a single .aspx file run to run on monos xsp/2, if anyone can 
put it on the webpage I think it would help.

<%@ Page Language="c#" Title="postgres via Npgsql" %>
<%@ Assembly name="System" %>
<%@ Assembly name="System.Data" %>
<%@ Assembly name="Npgsql" %>

<script runat="server">
		protected void Page_Load(object sender, EventArgs e) 
		{
			if (!Page.IsPostBack)
			{
		      string connectionString =
          		"Server=localhost;" +
          		"Database=test;" +
          		"User ID=postgres;" +
          		"Password=fun2db;";
		      	System.Data.IDbConnection dbcon;
		       	dbcon = new Npgsql.NpgsqlConnection(connectionString);
		       	dbcon.Open();
		       	System.Data.IDbCommand dbcmd = dbcon.CreateCommand();
       			string sql =
           			"SELECT firstname, lastname " +
           			"FROM employee";
		       	dbcmd.CommandText = sql;
		       	System.Data.IDataReader reader = dbcmd.ExecuteReader();
				dg.DataSource = reader;
				dg.DataBind();
			    reader.Close();
       			reader = null;
       			dbcmd.Dispose();
       			dbcmd = null;
       			dbcon.Close();
       			dbcon = null;
			}
		}
</script>

<html> 
<body>
The DataGrid that follows should list employees </ br>
<asp:DataGrid id="dg" runat="server">
</asp:DataGrid>
<pre>
       // requires a table to be created named employee
       // with columns firstname and lastname
       // such as,
       //        CREATE TABLE employee (
       //           firstname varchar(32),
       //           lastname varchar(32));
       //
       // create records thus
       //        INSERT INTO employee 
       //           SELECT 'Rob','Davis';
       //        INSERT INTO employee 
       //           SELECT 'Sarah','Davis';
</pre>
</body>
</html>  

Regards

Rob


More information about the Mono-docs-list mailing list