-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (70 loc) · 2.18 KB
/
Copy pathProgram.cs
File metadata and controls
77 lines (70 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NpgsqlIssue1362
{
public class Program
{
public static void Main(string[] args)
{
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder
{
Username = "user0",
Password = "password",
Database = "quotes",
Host = "192.168.106.128",
Port = 5432,
SslMode = SslMode.Require,
TrustServerCertificate = true,
SearchPath = "dbo"
};
string connStr = builder.ToString();
using (NpgsqlConnection connection = new NpgsqlConnection(connStr))
{
connection.Open();
SetupDatabase(connection);
WriteDataAsync(connection).Wait();
}
}
public static void SetupDatabase(NpgsqlConnection connection)
{
using (NpgsqlCommand command = connection.CreateCommand())
{
command.CommandText = @"
CREATE SCHEMA IF NOT EXISTS dbo ;
CREATE TABLE IF NOT EXISTS quotes (
quote_id serial PRIMARY KEY,
author VARCHAR(255),
quote TEXT
)";
command.ExecuteNonQuery();
}
}
public static async Task WriteDataAsync(NpgsqlConnection connection)
{
for (int i = 0; i < 5; ++i)
{
Console.Write("Attempt {0}: ", i + 1);
using (NpgsqlCommand command = connection.CreateCommand())
{
command.CommandText = @"
INSERT INTO quotes (
author,
quote
) VALUES (
'Jane Austen',
'It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.'
)";
int rowsInserted = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
if (rowsInserted == 1)
{
Console.WriteLine("Sucesss!");
}
}
}
}
}
}