5
5
6
6
namespace Database
7
7
{
8
- public class Database
8
+ public class SQLiteDataSource
9
9
{
10
10
string _connectionString = string . Empty ;
11
- string db_name = string . Empty ;
11
+ string _db_name = string . Empty ;
12
12
13
- SQLiteConnection _connection = new SQLiteConnection ( ) ;
14
- SQLiteCommand _command = new SQLiteCommand ( ) ;
13
+ public SQLiteConnection Connection { get ; set ; } = new SQLiteConnection ( ) ;
14
+ public SQLiteCommand Command { get ; set ; } = new SQLiteCommand ( ) ;
15
+ public SQLiteDataReader Reader { get ; set ; } = null ;
15
16
16
- public Database ( )
17
+ static Lazy < SQLiteDataSource > _database = new Lazy < SQLiteDataSource > ( ( ) =>
17
18
{
18
- db_name = ConfigurationManager . AppSettings [ "DatbaseFilepath" ] . ToString ( ) ;
19
- db_name = System . IO . Path . Combine ( System . Windows . Forms . Application . StartupPath , db_name ) ;
19
+ var db = new SQLiteDataSource ( ) ;
20
+ db . Initialize ( ) ;
20
21
21
- this . _connectionString = ConfigurationManager . AppSettings [ "connection" ] . ToString ( ) ;
22
- this . _connectionString = string . Format ( _connectionString , db_name ) ;
22
+ return db ;
23
+ } ) ;
23
24
24
- this . _connection . ConnectionString = this . _connectionString ;
25
+ public static SQLiteDataSource I => _database . Value ;
25
26
26
- //if (!System.IO.File.Exists(db_name))
27
- //{
28
- // SQLiteConnection.CreateFile(db_name);
29
- // _connection.Open();
30
- // _connection.Close();
31
- //}
32
- }
33
-
34
- public void Delete ( bool all )
27
+ public void Initialize ( )
35
28
{
36
- string dbpath = System . IO . Path . GetDirectoryName ( db_name ) ;
37
- try
38
- {
39
- foreach ( string f in System . IO . Directory . GetFiles ( dbpath ) )
40
- {
41
- if ( ! all )
42
- {
43
- if ( f . ToLower ( ) != this . db_name . ToLower ( ) )
44
- {
45
- System . IO . File . Delete ( f ) ;
46
- }
47
- }
48
- else
49
- {
50
- System . IO . File . Delete ( f ) ;
51
- }
52
- }
53
- }
54
- catch ( Exception ex )
55
- {
56
- System . Diagnostics . Debug . WriteLine ( ex . Message ) ;
57
- }
29
+ _db_name = ConfigurationManager . AppSettings [ "DatbaseFilepath" ] . ToString ( ) ;
30
+ _db_name = System . IO . Path . Combine ( System . Windows . Forms . Application . StartupPath , _db_name ) ;
31
+
32
+ _connectionString = ConfigurationManager . AppSettings [ "connection" ] . ToString ( ) ;
33
+ _connectionString = string . Format ( _connectionString , _db_name ) ;
34
+
35
+ this . Connection . ConnectionString = _connectionString ;
58
36
}
59
37
60
- public void ResetDatabase ( )
38
+ public void CloseConnection ( )
61
39
{
62
- Delete ( true ) ;
63
-
64
- System . Diagnostics . Debug . WriteLine ( "reseting database" ) ;
65
- ExecuteNonQuery ( DefaultDataAndSchema . sql , null ) ;
66
- System . Diagnostics . Debug . WriteLine ( "done" ) ;
40
+ this . Reader . Close ( ) ;
41
+ this . Command . Dispose ( ) ;
42
+ this . Connection . Close ( ) ;
67
43
}
44
+ }
68
45
69
- public string ExecuteQuery ( string sql_query , SQLiteParameter [ ] parameters , out SQLiteDataReader reader )
46
+ public class SQLiteDataController
47
+ {
48
+ public SQLiteDataSource Database => SQLiteDataSource . I ;
49
+
50
+ //public void Delete(bool all)
51
+ //{
52
+ // string dbpath = System.IO.Path.GetDirectoryName(db_name);
53
+ // try
54
+ // {
55
+ // foreach (string f in System.IO.Directory.GetFiles(dbpath))
56
+ // {
57
+ // if (!all)
58
+ // {
59
+ // if (f.ToLower() != this.db_name.ToLower())
60
+ // {
61
+ // System.IO.File.Delete(f);
62
+ // }
63
+ // }
64
+ // else
65
+ // {
66
+ // System.IO.File.Delete(f);
67
+ // }
68
+ // }
69
+ // }
70
+ // catch (Exception ex)
71
+ // {
72
+ // System.Diagnostics.Debug.WriteLine(ex.Message);
73
+ // }
74
+ //}
75
+
76
+ //public void ResetDatabase()
77
+ //{
78
+ // Delete(true);
79
+
80
+ // System.Diagnostics.Debug.WriteLine("reseting database");
81
+ // ExecuteNonQuery(DefaultDataAndSchema.sql, null);
82
+ // System.Diagnostics.Debug.WriteLine("done");
83
+ //}
84
+
85
+ public string ExecuteQuery ( string sql_query , SQLiteParameter [ ] parameters )
70
86
{
71
87
string res = string . Empty ;
72
88
73
- reader = null ;
74
-
75
- //using(SQLiteConnection conn = new SQLiteConnection(this._connectionString))
76
- //{
77
- // conn.Open();
78
- // using (SQLiteCommand comm = conn.CreateCommand())
79
- // {
80
- // comm.CommandText = sql_query;
81
-
82
- // if(parameters != null)
83
- // {
84
- // comm.Parameters.AddRange(parameters);
85
- // }
86
-
87
- // try
88
- // {
89
- // this._command = comm;
90
-
91
- // reader = this._command.ExecuteReader(CommandBehavior.CloseConnection);
92
- // }
93
- // catch (Exception ex)
94
- // {
95
- // res = CreateExceptionMessage(ex);
96
- // }
97
- // }
98
- //}
99
-
100
- reader = null ;
101
-
102
- //if (_connection.State == ConnectionState.Open)
103
- //{
104
- // CloseConnection();
105
- //}
106
-
107
- _connection = new SQLiteConnection ( this . _connectionString ) ;
108
- _connection . Open ( ) ;
109
- _command = _connection . CreateCommand ( ) ;
110
- _command . CommandText = sql_query ;
89
+ this . Database . Connection . Open ( ) ;
90
+
91
+ this . Database . Command = this . Database . Connection . CreateCommand ( ) ;
92
+ this . Database . Command . CommandText = sql_query ;
93
+ this . Database . Command . Parameters . Clear ( ) ;
111
94
112
95
if ( parameters != null )
113
- {
114
- _command . Parameters . Clear ( ) ;
115
- _command . Parameters . AddRange ( parameters ) ;
116
- }
96
+ this . Database . Command . Parameters . AddRange ( parameters ) ;
117
97
118
98
try
119
99
{
120
- reader = _command . ExecuteReader ( CommandBehavior . CloseConnection ) ;
100
+ this . Database . Reader = this . Database . Command . ExecuteReader ( CommandBehavior . CloseConnection ) ;
121
101
}
122
102
catch ( Exception ex )
123
103
{
@@ -131,71 +111,30 @@ public string ExecuteNonQuery(string sql_query, SQLiteParameter[] parameters)
131
111
{
132
112
string res = string . Empty ;
133
113
134
- //using (SQLiteConnection conn = new SQLiteConnection(this._connectionString))
135
- //{
136
- // conn.Open();
137
- // using (SQLiteCommand comm = conn.CreateCommand())
138
- // {
139
- // comm.CommandText = sql_query;
140
-
141
- // if (parameters != null)
142
- // {
143
- // comm.Parameters.AddRange(parameters);
144
- // }
145
-
146
- // try
147
- // {
148
- // comm.ExecuteNonQuery();
149
- // }
150
- // catch (Exception ex)
151
- // {
152
- // res = CreateExceptionMessage(ex);
153
- // }
154
- // }
155
- //}
156
-
157
- //if (_connection.State == ConnectionState.Open)
158
- //{
159
- // CloseConnection();
160
- //}
161
-
162
- _connection = new SQLiteConnection ( this . _connectionString ) ;
163
- _connection . Open ( ) ;
164
- _command = _connection . CreateCommand ( ) ;
165
- _command . CommandText = sql_query ;
114
+ this . Database . Connection . Open ( ) ;
115
+
116
+ this . Database . Command = this . Database . Connection . CreateCommand ( ) ;
117
+ this . Database . Command . CommandText = sql_query ;
118
+ this . Database . Command . Parameters . Clear ( ) ;
166
119
167
120
if ( parameters != null )
168
- {
169
- _command . Parameters . AddRange ( parameters ) ;
170
- }
121
+ this . Database . Command . Parameters . AddRange ( parameters ) ;
171
122
172
123
try
173
124
{
174
- _command . ExecuteNonQuery ( ) ;
125
+ this . Database . Command . ExecuteNonQuery ( ) ;
175
126
}
176
127
catch ( Exception ex )
177
128
{
178
129
System . Diagnostics . Debug . WriteLine ( "ERROR! " + ex . Message + "\r \n " + ex . StackTrace ) ;
179
130
res = CreateExceptionMessage ( ex ) ;
180
131
}
181
132
182
- _connection . Close ( ) ;
133
+ //this.Database.CloseConnection ();
183
134
184
135
return res ;
185
136
}
186
137
187
- public void CloseConnection ( )
188
- {
189
- _command . Dispose ( ) ;
190
-
191
- if ( _connection != null )
192
- {
193
- _connection . Close ( ) ;
194
- _connection . Dispose ( ) ;
195
- _connection = null ;
196
- }
197
- }
198
-
199
138
private string CreateExceptionMessage ( Exception ex )
200
139
{
201
140
string err = string . Empty ;
0 commit comments