Hermes/DataAccess.cs
2020-09-07 22:24:37 +02:00

45 lines
1.3 KiB
C#

using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace Hermes
{
public static class DataAccess
{
private static SqliteConnection db= null;
public static SqliteConnection GetConnection() {
if (db != null) {
return db;
}
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hermes.db");
if (!System.IO.File.Exists(dbpath)) {
FileStream fs = File.Create(dbpath);
fs.Close();
}
db = new SqliteConnection($"Filename={dbpath}");
db.Open();
var createTableCmd = "CREATE TABLE IF NOT EXISTS citoyen (id INTEGER PRIMARY KEY, nom TEXT NOT NULL, prenom TEXT NOT NULL, date_naissance TEXT NOT NULL, adresse TEXT NOT NULL, profession TEXT, type_residence INTEGER NOT NULL, mail TEXT, tel TEXT)";
SqliteCommand createTable = new SqliteCommand(createTableCmd, db);
createTable.ExecuteReader();
return db;
}
public static void CloseConnection() {
if (db != null) {
db.Close();
db = null;
}
}
}
}