· 5 years ago · Aug 12, 2020, 07:50 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Data.SQLite;
7using System.IO;
8using System.Drawing;
9using System.Diagnostics;
10
11namespace OOPWork
12{
13 class Program
14 {
15 static void Main(string[] args)
16 {
17 //Database initdb = new Database();
18 Database initdb1 = new Database("PlayerDatabaseFull");
19 initdb1.CreateTable("PlayersList", "PlayerDatabaseFull");
20 initdb1.ADDPlayer("PlayersList", "PlayerDatabaseFull", "Pavel", 20);
21 initdb1.ADDPlayer("PlayersList", "PlayerDatabaseFull", "Lox", 20);
22 initdb1.DeletePlayer("PlayersList", "PlayerDatabaseFull",1);
23 initdb1.BanPlayer("PlayersList", "PlayerDatabaseFull", 3);
24 initdb1.UnbanPlayer("PlayersList", "PlayerDatabaseFull", 2);
25 }
26 }
27
28 class Database
29 {
30 public Database()
31 {
32 if (!File.Exists(@".\PlayersDB.db"))
33 {
34 SQLiteConnection.CreateFile(@".\PlayersDB.db");
35 }
36 }
37
38 public Database(string name)
39 {
40 if (!File.Exists($@".\{name}.db"))
41 {
42 SQLiteConnection.CreateFile($@".\{name}.db");
43 }
44 }
45
46 private void ExecuteQuery(string dbName, string sqlQuery)
47 {
48 using (SQLiteConnection Connect = new SQLiteConnection($@"Data Source=.\{dbName}.db; Version=3;"))
49 {
50 SQLiteCommand Command = new SQLiteCommand(sqlQuery, Connect);
51 Connect.Open();
52 Command.ExecuteNonQuery();
53 Connect.Close();
54 }
55 }
56
57 public void CreateTable(string tableName, string dbName)
58 {
59 string commandText = $"CREATE TABLE IF NOT EXISTS [{tableName}] ( [id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [NickName] VARCHAR(50), [Level] INT,BanFlag BOOLEAN NOT NULL CHECK (BanFlag IN (0,1)) )";
60 ExecuteQuery(dbName, commandText);
61 }
62
63 public void ADDPlayer(string tableName, string dbName,string nickName,int level)
64 {
65 string commandText = $@"INSERT INTO [{tableName}] (NickName, Level, BanFlag) VALUES('{nickName}','{level}','0')";
66 ExecuteQuery(dbName, commandText);
67 }
68
69 public void DeletePlayer(string tableName, string dbName,int id)
70 {
71 string commandText = $@"DELETE FROM [{tableName}] WHERE [id] = '{id}'";
72 ExecuteQuery(dbName, commandText);
73 }
74
75 public void BanPlayer(string tableName, string dbName, int id)
76 {
77 string commandText = $@"UPDATE [{tableName}] SET [BanFlag] = '1' WHERE [id] = '{id}'";
78 ExecuteQuery(dbName, commandText);
79 }
80
81 public void UnbanPlayer(string tableName, string dbName, int id)
82 {
83 string commandText = $@"UPDATE [{tableName}] SET [BanFlag] = '0' WHERE [id] = '{id}'";
84 ExecuteQuery(dbName, commandText);
85 }
86 }
87}
88