mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Restructure data access layer/data layer
This commit is contained in:
55
DataAccessLayer/Sql/DatabaseHelper.cs
Normal file
55
DataAccessLayer/Sql/DatabaseHelper.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace DataAccessLayer.Sql
|
||||
{
|
||||
public class DatabaseHelper
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public DatabaseHelper(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public void ExecuteRawSql(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (
|
||||
SqlConnection connection = new SqlConnection(
|
||||
_connectionString
|
||||
)
|
||||
)
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
using (
|
||||
SqlCommand command = new SqlCommand(query, connection)
|
||||
)
|
||||
{
|
||||
command.CommandType = CommandType.Text;
|
||||
|
||||
using (SqlDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"{reader.GetName(i)}: {reader.GetValue(i)}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"An error occurred: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user