Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (83 loc) · 3.21 KB
/
Copy pathProgram.cs
File metadata and controls
92 lines (83 loc) · 3.21 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
using System;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;
namespace ePharmaTrack
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// ✅ Initialize both user and sales databases before running the UI
InitializeUserDatabase();
InitializeSalesDatabase();
// ✅ Start the login form
Application.Run(new LoginForm());
}
// ---------- Create Users.db if not exists ----------
private static void InitializeUserDatabase()
{
string userDbPath = "users.db";
if (!File.Exists(userDbPath))
{
SQLiteConnection.CreateFile(userDbPath);
}
using (SQLiteConnection con = new SQLiteConnection($"Data Source={userDbPath};Version=3;"))
{
con.Open();
string sql = @"CREATE TABLE IF NOT EXISTS Users(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Username TEXT NOT NULL UNIQUE,
Password TEXT NOT NULL,
Role TEXT NOT NULL)";
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
{
cmd.ExecuteNonQuery();
}
// Insert default admin if none exists
sql = "SELECT COUNT(*) FROM Users";
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
{
long count = (long)cmd.ExecuteScalar();
if (count == 0)
{
string insertSql = @"INSERT INTO Users (Username, Password, Role)
VALUES ('admin', '1234', 'Admin')";
using (SQLiteCommand insertCmd = new SQLiteCommand(insertSql, con))
{
insertCmd.ExecuteNonQuery();
}
}
}
}
}
// ---------- Create Sales.db if not exists ----------
private static void InitializeSalesDatabase()
{
string salesDbPath = "sales.db";
if (!File.Exists(salesDbPath))
{
SQLiteConnection.CreateFile(salesDbPath);
}
using (SQLiteConnection conn = new SQLiteConnection($"Data Source={salesDbPath};Version=3;"))
{
conn.Open();
string sql = @"CREATE TABLE IF NOT EXISTS Sales(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
MedicineName TEXT NOT NULL,
QuantitySold INTEGER NOT NULL,
SaleDate TEXT NOT NULL)";
using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}
}
}
}
}