Mise en place de la structure définitive du CRUD et modification de la page principale avec les datas
This commit is contained in:
36
Helpers/DbHelper.cs
Normal file
36
Helpers/DbHelper.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
public static class DbHelper
|
||||
{
|
||||
public static int ExecuteNonQuery(string query, Dictionary<string, object> parameters)
|
||||
{
|
||||
using var connection = new SqlConnection(AppSettings.GetConnectionString("DefaultConnection"));
|
||||
connection.Open();
|
||||
|
||||
using var command = new SqlCommand(query, connection);
|
||||
foreach (var param in parameters)
|
||||
command.Parameters.AddWithValue(param.Key, param.Value ?? DBNull.Value);
|
||||
|
||||
return command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static List<Dictionary<string, object>> ExecuteQuery(string query)
|
||||
{
|
||||
using var connection = new SqlConnection(AppSettings.GetConnectionString("DefaultConnection"));
|
||||
connection.Open();
|
||||
|
||||
using var command = new SqlCommand(query, connection);
|
||||
using var reader = command.ExecuteReader();
|
||||
|
||||
var result = new List<Dictionary<string, object>>();
|
||||
while (reader.Read())
|
||||
{
|
||||
var row = new Dictionary<string, object>();
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
row[reader.GetName(i)] = reader.GetValue(i);
|
||||
result.Add(row);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user