Created
January 3, 2024 14:04
-
-
Save cabecada/dd47f1549c5aac46ab20667c44139d23 to your computer and use it in GitHub Desktop.
npgsql example postgres
This file contains 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 System.Collections; | |
using Npgsql; | |
namespace Program | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) // without async task main, cannot use await | |
{ | |
var dataSourceBuilder = new NpgsqlDataSourceBuilder("Host=localhost;Username=postgres;Password=postgres;Database=postgres"); | |
await using var dataSource = dataSourceBuilder.Build(); | |
await using (var command = dataSource.CreateCommand(@" | |
create table if not exists foo (col1 int, ""COL2"" int); | |
insert into foo select x,x from generate_series(1, 10) x; | |
create table if not exists bar as (select * from foo); | |
" | |
)) | |
{ | |
await command.ExecuteNonQueryAsync(); | |
} | |
await using (var cmd = dataSource.CreateCommand(@"select col1,""COL2"" from bar;")) | |
await using (var reader = await cmd.ExecuteReaderAsync()) | |
{ | |
while (await reader.ReadAsync()) | |
{ | |
Console.WriteLine( "" + reader.GetInt64(0) +""+ reader.GetInt64(1) +"") ; | |
} | |
} | |
} | |
/* | |
static async Task Main(string[] args) | |
{ | |
var connectionString = "Host=localhost;Username=postgres;Password=postgres;Database=postgres"; | |
await using var dataSource = NpgsqlDataSource.Create(connectionString); | |
// Insert some data | |
await using (var cmd = dataSource.CreateCommand("INSERT INTO demo (name) VALUES ($1)")) | |
{ | |
cmd.Parameters.AddWithValue("Hello world"); | |
await cmd.ExecuteNonQueryAsync(); | |
} | |
// Retrieve all rows | |
await using (var cmd = dataSource.CreateCommand("SELECT name FROM demo")) | |
await using (var reader = await cmd.ExecuteReaderAsync()) | |
{ | |
while (await reader.ReadAsync()) | |
{ | |
Console.WriteLine(reader.GetString(0)); | |
} | |
} | |
} | |
*/ | |
/* | |
static async Task Main(string[] args) | |
{ | |
Task<int> result = LongProcess(); | |
ShortProcess(); | |
var val = await result; // wait untile get the return value | |
Console.WriteLine("Result: {0}", val); | |
Console.ReadKey(); | |
} | |
static async Task<int> LongProcess() | |
{ | |
Console.WriteLine("LongProcess Started"); | |
await Task.Delay(4000); // hold execution for 4 seconds | |
Console.WriteLine("LongProcess Completed"); | |
return 10; | |
} | |
static void ShortProcess() | |
{ | |
Console.WriteLine("ShortProcess Started"); | |
//do something here | |
Console.WriteLine("ShortProcess Completed"); | |
} | |
*/ | |
/* | |
static void Main(string[] args) | |
{ | |
var connectionString = "Host=localhost;Username=postgres;Password=postgres;Database=postgres;Port=5432;Timeout=10;Command Timeout=10"; | |
using (var connection = new NpgsqlConnection(connectionString)) | |
{ | |
connection.Open(); | |
var databaseTable = "numtable"; | |
using (var cmd = new NpgsqlCommand()) | |
{ | |
cmd.Connection = connection; | |
cmd.CommandText = "CREATE TABLE IF NOT EXISTS " + databaseTable + | |
" (id SERIAL PRIMARY KEY, number INTEGER NOT NULL UNIQUE)"; | |
cmd.ExecuteNonQuery(); | |
} | |
var arrayNumbers = new ArrayList { 17, 27, 37, 43, 10, 88, 16, 23, 88 }; | |
foreach (var number in arrayNumbers) | |
{ | |
using (var cmd = new NpgsqlCommand()) | |
{ | |
cmd.Connection = connection; | |
cmd.CommandText = "INSERT INTO " + databaseTable + | |
" (number) VALUES (" + number + ") ON CONFLICT (number) DO NOTHING"; | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
using (var cmd = new NpgsqlCommand("SELECT * FROM " + databaseTable, connection)) | |
{ | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
Console.WriteLine("|---------------|---------------|"); | |
Console.WriteLine("| id | number |"); | |
Console.WriteLine("|---------------|---------------|"); | |
while (reader.Read()) | |
{ | |
Console.Write("|\t{0}\t|\t{1}\t|\n", reader[0], reader[1]); | |
Console.WriteLine("|---------------|---------------|"); | |
} | |
} | |
} | |
using (var cmd = new NpgsqlCommand("SELECT 1,2 from pg_sleep(20)", connection)) | |
{ | |
using (var reader = cmd.ExecuteReader()) | |
{ | |
Console.WriteLine("|---------------|---------------|"); | |
Console.WriteLine("| id | number |"); | |
Console.WriteLine("|---------------|---------------|"); | |
while (reader.Read()) | |
{ | |
Console.Write("|\t{0}\t|\t{1}\t|\n", reader[0], reader[1]); | |
Console.WriteLine("|---------------|---------------|"); | |
} | |
} | |
} | |
connection.Close(); | |
} | |
} | |
*/ | |
} | |
} |
Author
cabecada
commented
Jan 6, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment