-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
executable file
·248 lines (187 loc) · 6.79 KB
/
Database.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SQLite;
using LumenWorks.Framework.IO.Csv;
using System.Data.Common;
namespace Chromophobia
{
public class Database
{
protected static String Dbfile;
private SQLiteConnection Conn;
public Database()
{
Conn = new SQLiteConnection();
Dbfile = App.Path + @"data\chromo.db";
}
private void Open()
{
try
{
Conn.Open();
}
catch (SQLiteException e)
{
App.Log("<b>" + e.Message + ", DB File: "+Dbfile+"</b>");
throw (new Exception("Database Error"));
}
}
private void Init()
{
if (Conn.State != System.Data.ConnectionState.Open)
{
Conn.ConnectionString = "Data Source=" + Dbfile+ ";Version=3";
Open();
}
}
public void Close()
{
if (Conn.State == System.Data.ConnectionState.Open) Conn.Close();
}
public void OpenFast()
{
if (Conn.State == System.Data.ConnectionState.Open) Conn.Close();
Conn = new SQLiteConnection();
Conn.ConnectionString = "Data Source=" + Dbfile + ";Version=3;Count Changes=off;" +
"Journal Mode=off;Pooling=true;Cache Size=10000;Page Size=4096;Synchronous=0";
Open();
}
~Database()
{
Close();
}
public void Exec(String sql)
{
Init();
SQLiteCommand MyCmd = new SQLiteCommand(sql, Conn);
//MyCmd.ExecuteScalar();
MyCmd.ExecuteNonQuery();
}
public DbDataReader Query(String sql)
{
Init();
SQLiteCommand cmd = new SQLiteCommand(sql, Conn);
SQLiteDataReader reader = cmd.ExecuteReader();
return reader;
}
public DbDataReader QueryRead(String sql)
{
Init();
SQLiteCommand cmd = new SQLiteCommand(sql, Conn);
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
}
return reader;
}
public SQLiteCommand CreateCommand()
{
return Conn.CreateCommand();
}
public SQLiteTransaction BeginTransaction()
{
return Conn.BeginTransaction();
}
public CsvReader OpenCsv(String fileName)
{
return new CsvReader(new StreamReader(fileName), false, '\t');
}
public void ImportCsvOld(String fileName, String table, Dictionary<string, int> fields)
{
OpenFast();
Exec("DELETE FROM " + table);
int c = 0;
using (CsvReader csv = OpenCsv(fileName))
{
int fieldCount = csv.FieldCount;
while (csv.ReadNextRecord())
{
String sql = "INSERT INTO " + table + "(" + String.Join(",", fields.Keys) + ") VALUES (";
List<String> vals = new List<String>();
foreach (int key in fields.Values)
{
String val = csv[key];
val = val.Replace("'", "''");
vals.Add("'" + val + "'");
}
sql += String.Join(",", vals) + ")";
// App.Log(sql);
Exec(sql);
c++;
//if (c > 10) break;
}
}
App.Log(c.ToString() + " Datensätze aus " + fileName + " in " + table + " eingefügt");
Conn.Close();
}
public void ImportCsv(String fileName, String table, Dictionary<string, int> fields)
{
OpenFast();
var lineCount = 0;
using (var reader = File.OpenText(fileName))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}
Exec("DELETE FROM " + table);
// Exec("PRAGMA synchronous=0");
int c = 0;
using (CsvReader csv = new CsvReader(new StreamReader(fileName), false, '\t'))
{
int fieldCount = csv.FieldCount;
SQLiteCommand command = Conn.CreateCommand();
SQLiteTransaction transaction = Conn.BeginTransaction();
command.Transaction = transaction;
String sql = "INSERT INTO " + table + "(" + String.Join(",", fields.Keys) + ") VALUES (";
List<String> vals = new List<String>();
foreach (String field in fields.Keys) vals.Add("@" + field + "");
sql += String.Join(",", vals) + ")";
command.CommandText = sql;
/*
foreach (String field in fields.Keys)
{
command.Parameters.AddWithValue("@" + field, "");
}
*/
while (csv.ReadNextRecord())
{
foreach (String field in fields.Keys)
{
String val = csv[fields[field]];
val = val.Replace("'", "''");
command.Parameters.AddWithValue("@"+field, val);
//command.Parameters["@"+field].Value = val;
}
command.ExecuteNonQuery();
//if (c > 10) break;
/* ctrans++;
if (ctrans > 10000)
{
transaction.Commit();
transaction.Dispose();
transaction = Conn.BeginTransaction();
ctrans = 0;
}*/
c++;
if ((c % 100) == 0)
{
int percent = (int)(((float)c / (float)lineCount) * 100);
//.Document.InvokeScript("writeElement", new String[] { sHtml, sHtmlElementId });
App.Status("Table " + table + ": " + percent.ToString() + "% - " + c.ToString() + " of " + lineCount.ToString());
}
}
transaction.Commit();
command.Dispose();
App.Status();
App.Log(c.ToString() + " Datensätze aus " + fileName + " in " + table + " eingefügt");
}
Conn.Close();
}
}
}