-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalDb.cs
More file actions
214 lines (193 loc) · 6.93 KB
/
GlobalDb.cs
File metadata and controls
214 lines (193 loc) · 6.93 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.Json; //Nuget-Package System.Text.Json installieren
using static DB_Helper.NotificationLibrary;
namespace DB_Helper
{
public class DbHelper
{
private static readonly DbHelper db = GetDb();
public static DataTable SqlGet(string sql) => db.SqlGetData(sql);
public static DataTable SqlGet(string sql, params SqlParameter[] parameters) => db.SqlGetData(sql, parameters);
public static int SqlSet(string sql) => db.SqlSetData(sql);
public static int SqlSet(string sql, params SqlParameter[] parameters) => db.SqlSetData(sql, parameters);
private string _connectionString;
private DbHelper()
{
_connectionString = LoadConnectionString();
}
private string LoadConnectionString()
{
var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dbconfig.json");
if (!File.Exists(configPath))
{
var console = new NotificationLibrary.ConsoleNotification();
console.ShowWarning("Keine Config File gefunden");
var logger = new LogNotification("app.log");
logger.ShowError($"Kein Config File gefunden");
return "";
}
var jsonString = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<DatabaseConfig>(jsonString);
var builder = new SqlConnectionStringBuilder
{
DataSource = config.DatabaseSettings.Server,
InitialCatalog = config.DatabaseSettings.Database,
IntegratedSecurity = config.DatabaseSettings.IntegratedSecurity,
TrustServerCertificate = config.DatabaseSettings.TrustServerCertificate
};
if (!config.DatabaseSettings.IntegratedSecurity)
{
builder.UserID = config.DatabaseSettings.Username;
builder.Password = config.DatabaseSettings.Password;
}
return builder.ToString();
}
public class DatabaseConfig
{
public DatabaseSettings DatabaseSettings { get; set; }
}
public class DatabaseSettings
{
public string Server { get; set; }
public string Database { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool IntegratedSecurity { get; set; }
public bool TrustServerCertificate { get; set; }
}
private static DbHelper _instance;
public static DbHelper GetDb()
{
if (_instance == null)
{
_instance = new DbHelper();
}
return _instance;
}
// Einfache Version für SELECT
public DataTable SqlGetData(string sqlBefehl)
{
DataTable dataTable = new DataTable();
SqlConnection verbindung = null;
try
{
verbindung = new SqlConnection(_connectionString);
SqlCommand command = new SqlCommand(sqlBefehl, verbindung);
SqlDataAdapter adapter = new SqlDataAdapter(command);
verbindung.Open();
adapter.Fill(dataTable);
return dataTable;
}
catch (Exception ex)
{
var logger = new LogNotification("app.log");
logger.ShowError($"Datenbankfehler: {ex.Message}");
return null;
}
finally
{
if (verbindung != null)
{
verbindung.Close();
verbindung.Dispose();
}
}
}
// Version mit Parametern für SELECT
public DataTable SqlGetData(string sqlBefehl, params SqlParameter[] parameters)
{
DataTable dataTable = new DataTable();
SqlConnection verbindung = null;
try
{
verbindung = new SqlConnection(_connectionString);
SqlCommand command = new SqlCommand(sqlBefehl, verbindung);
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
SqlDataAdapter adapter = new SqlDataAdapter(command);
verbindung.Open();
adapter.Fill(dataTable);
return dataTable;
}
catch (Exception ex)
{
var logger = new LogNotification("app.log");
logger.ShowError($"Datenbankfehler: {ex.Message}");
return null;
}
finally
{
if (verbindung != null)
{
verbindung.Close();
verbindung.Dispose();
}
}
}
// Einfache Version für INSERT, UPDATE, DELETE
public int SqlSetData(string sqlBefehl)
{
SqlConnection verbindung = null;
try
{
verbindung = new SqlConnection(_connectionString);
SqlCommand command = new SqlCommand(sqlBefehl, verbindung);
verbindung.Open();
return command.ExecuteNonQuery();
}
catch (Exception ex)
{
var logger = new LogNotification("app.log");
logger.ShowError($"Datenbankfehler: {ex.Message}");
return -1;
}
finally
{
if (verbindung != null)
{
verbindung.Close();
verbindung.Dispose();
}
}
}
// Version mit Parametern für INSERT, UPDATE, DELETE
public int SqlSetData(string sqlBefehl, params SqlParameter[] parameters)
{
SqlConnection verbindung = null;
try
{
verbindung = new SqlConnection(_connectionString);
SqlCommand command = new SqlCommand(sqlBefehl, verbindung);
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
verbindung.Open();
return command.ExecuteNonQuery();
}
catch (Exception ex)
{
var logger = new LogNotification("app.log");
logger.ShowError($"Datenbankfehler: {ex.Message}");
return -1;
}
finally
{
if (verbindung != null)
{
verbindung.Close();
verbindung.Dispose();
}
}
}
}
}