2chBackConsole/DBLinks.cs

125 lines
4.2 KiB
C#
Raw Normal View History

2022-10-14 20:00:11 +10:00
using System;
using System.Xml.Linq;
using Npgsql;
namespace _2chSQL
{
public class DBLinks
{
// Obtain connection string information from the portal
//
private static string Host = "postgres.vdk2ch.ru";
private static string User = "postgres";
private static string DBname = "postgres";
private static string Password = "postgres";
private static string Port = "5432";
public static void DbList()
{
// Build connection string using parameters from portal
//
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host,
User,
DBname,
Port,
Password);
using (var conn = new NpgsqlConnection(connString))
{
2022-10-14 23:45:38 +10:00
//Console.Out.WriteLine("Opening connection");
2022-10-14 20:00:11 +10:00
conn.Open();
2022-10-15 15:45:26 +10:00
using (var command = new NpgsqlCommand("SELECT * FROM (SELECT post_id, substring(post,1,200) FROM dvach ORDER BY post_id DESC limit 10) subquery ORDER BY post_id ASC", conn))
2022-10-14 20:00:11 +10:00
{
var reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(
string.Format(
2022-10-15 15:45:26 +10:00
"#{0}: {1}",
2022-10-14 20:00:11 +10:00
reader.GetInt32(0).ToString(),
reader.GetString(1)
//reader.GetString(2)
)
);
}
reader.Close();
}
}
2022-10-15 15:45:26 +10:00
//Console.WriteLine("На этом вся таблица!");
2022-10-14 20:00:11 +10:00
}
public static void DbUpdate(string id, string text)
{
// Build connection string using parameters from portal
//
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host,
User,
DBname,
Port,
Password);
using (var conn = new NpgsqlConnection(connString))
{
2022-10-14 23:45:38 +10:00
//Console.Out.WriteLine("Opening connection");
2022-10-14 20:00:11 +10:00
conn.Open();
using (var command = new NpgsqlCommand("UPDATE dvach " +
"SET post = @postText WHERE post_id = @ID", conn))
{
command.Parameters.AddWithValue("ID", int.Parse(id));
command.Parameters.AddWithValue("postText", text);
int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
}
}
Console.WriteLine("Данные обновлены!");
Console.ReadLine();
}
public static void DbAdd(string text)
{
// Build connection string using parameters from portal
//
string connString =
String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host,
User,
DBname,
Port,
Password);
using (var conn = new NpgsqlConnection(connString))
{
Console.Out.WriteLine("Opening connection");
conn.Open();
using (var command = new NpgsqlCommand("INSERT INTO dvach " +
"(post_id, post) VALUES (DEFAULT, @postText)", conn))
{
command.Parameters.AddWithValue("postText", text);
int nRows = command.ExecuteNonQuery();
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
}
}
Console.WriteLine("С добавлением закончено");
}
}
}