-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLiteHelper.cs
More file actions
68 lines (59 loc) · 2.33 KB
/
Copy pathSQLiteHelper.cs
File metadata and controls
68 lines (59 loc) · 2.33 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
namespace GrpcServerConsole
{
using Microsoft.Data.Sqlite;
using System.IO;
public class SQLiteHelper
{
private string _dbPath;
private string _tableName;
public SQLiteHelper(string dbPath)
{
_dbPath = dbPath;
}
public void InitializeTable(string tableName)
{
_tableName = tableName;
using (var connection = new SqliteConnection($"Data Source={_dbPath}"))
{
connection.Open();
string tableCommand = @$"
CREATE TABLE IF NOT EXISTS {_tableName} (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
CategoryName TEXT,
ElemName TEXT,
ElemGuid TEXT,
GeomParameters TEXT,
DataParameters TEXT
)";
using (var createTable = new SqliteCommand(tableCommand, connection))
{
createTable.ExecuteNonQuery();
}
}
}
public void InsertElement(string fileName, string categoryName, string elemName, string elemGuid, string geomParameters, string dataParameters)
{
if (fileName != _tableName)
{
_tableName = fileName;
InitializeTable(_tableName);
}
using (var connection = new SqliteConnection($"Data Source={_dbPath}"))
{
connection.Open();
string insertCommand = @$"
INSERT INTO {_tableName} (CategoryName, ElemName, ElemGuid, GeomParameters, DataParameters)
VALUES (@CategoryName, @ElemName, @ElemGuid, @GeomParameters, @DataParameters)";
using (var insert = new SqliteCommand(insertCommand, connection))
{
insert.Parameters.AddWithValue("@CategoryName", categoryName);
insert.Parameters.AddWithValue("@ElemName", elemName);
insert.Parameters.AddWithValue("@ElemGuid", elemGuid);
insert.Parameters.AddWithValue("@GeomParameters", geomParameters);
insert.Parameters.AddWithValue("@DataParameters", dataParameters);
insert.ExecuteNonQuery();
}
}
}
}
}