Created
August 30, 2018 07:38
-
-
Save fearofcode/0a7f7a16611df7f787cdf667d29cab4d to your computer and use it in GitHub Desktop.
Simple working example of working with Npgsql.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Npgsql; | |
namespace learningcsharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connString = "Host=localhost;Username=arete;Database=arete"; | |
using (var conn = new NpgsqlConnection(connString)) | |
{ | |
conn.Open(); | |
using (var cmd = new NpgsqlCommand()) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandText = "INSERT INTO customers (first_name, last_name) VALUES (@first_name, @last_name)"; | |
cmd.Parameters.AddWithValue("first_name", "Warren"); | |
cmd.Parameters.AddWithValue("last_name", "Henning"); | |
cmd.Prepare(); | |
cmd.ExecuteNonQuery(); | |
} | |
DateTime start = DateTime.Now; | |
using (var cmd = new NpgsqlCommand("SELECT id, first_name, last_name FROM customers order by id desc LIMIT 5", conn)) | |
{ | |
cmd.Prepare(); | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
Console.WriteLine("{0}: {1}, {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2)); | |
} | |
} | |
} | |
DateTime end = DateTime.Now; | |
Console.WriteLine(end - start); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment