Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created October 4, 2013 22:14
Show Gist options
  • Select an option

  • Save hyrmn/6833706 to your computer and use it in GitHub Desktop.

Select an option

Save hyrmn/6833706 to your computer and use it in GitHub Desktop.
Showing cats with Dapper and PostgreSQL and NancyFX
CREATE DATABASE demo
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'English_United States.1252'
LC_CTYPE = 'English_United States.1252'
CONNECTION LIMIT = -1;
CREATE TABLE cats
(
catid serial NOT NULL,
name character varying(50) NOT NULL,
CONSTRAINT pk_cats PRIMARY KEY (catid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE cats
OWNER TO postgres;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NancyApplication1</title>
</head>
<body>
Cats:<br/>
@foreach (var cat in Model.Cats)
{
@cat.name <br />
}
</body>
</html>
using System.Data;
using Npgsql;
using Nancy;
using Dapper;
namespace NancyApplication1
{
public class IndexModule : NancyModule
{
private const string ConnectionString = "Server=localhost;User Id=postgres;password=Postgres1234;database=demo";
public IndexModule()
{
Get["/"] = (ಠʃಠ) =>
{
using (IDbConnection connection = new NpgsqlConnection(ConnectionString))
{
var cats = connection.Query("select catId, Name from cats");
return View["index", new {Cats = cats}];
}
};
}
}
}
INSERT INTO cats(name)
VALUES ('Bob');
INSERT INTO cats(name)
VALUES ('Garfield');
INSERT INTO cats(name)
VALUES ('Nermele');
INSERT INTO cats(name)
VALUES ('Sue Ellen');
INSERT INTO cats(name)
VALUES ('Patches');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment