Repository navigation
Expand file tree
/
Copy pathDatabase.cs
More file actions
107 lines (94 loc) · 3.27 KB
/
Copy pathDatabase.cs
File metadata and controls
107 lines (94 loc) · 3.27 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
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PatientBillingSystem
{
internal class Database
{
// SQL DATABASE
private static string connectionString = """
Server=LAPTOP-ASTRODAV;
Database=PatientBillingSystem;
Integrated Security=True;
Encrypt=True;
TrustServerCertificate=True;
""";
public static SqlConnection SqlConnection = new SqlConnection(connectionString);
// METHODS
public static void StartSqlConnection()
{
if (SqlConnection == null) SqlConnection = new SqlConnection(connectionString);
SqlConnection.Open();
}
public static void StopSqlConnection()
{
if(SqlConnection != null) SqlConnection.Close();
}
public static bool IsSqlConnected()
{
if (SqlConnection.State == System.Data.ConnectionState.Open) return true;
else
{
MessageBox.Show("The system is not connected to database", "SQL Connection Failed");
return false;
}
}
// SQL QUERY
public static List<string[]> Query(string query)
{
try
{
SqlConnection.Open();
if (!IsSqlConnected())
{
Debug.WriteLine("Database not connected. return NULL??");
return null;
}
SqlCommand command = new SqlCommand(query, SqlConnection);
if (query.Trim().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
{
SqlDataReader reader = command.ExecuteReader();
List<string[]> rows = new List<string[]>();
while (reader.Read())
{
string[] row = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
row[i] = reader.GetValue(i).ToString();
rows.Add(row);
}
if (rows.Count == 0)
{
string[] row = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
row[i] = string.Empty;
rows.Add(row);
}
reader.Close();
SqlConnection.Close();
return rows;
}
else
{
command.ExecuteNonQuery();
SqlConnection.Close();
return null;
}
}
catch (Exception e)
{
SqlConnection.Close();
throw e;
}
}
// Project developed by Dave Laurence Tolentino | BSIT-3B
// FINAL TASK PERFORMANCE FOR
// EVENT-DRIVEN PROGRAMMING | ADVANCED DATABASE | APPLICATION DEVELOPMENT
// Final revision submitted at <insert date>
}
}