-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteOps.cs
86 lines (70 loc) · 2.58 KB
/
SQLiteOps.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SQLite;
using Dapper;
namespace CodeGitz
{
internal static class SQLiteOps
{
// Creates a Table From The String Parameter
public static void CreateTable(String CreateStatement)
{
// Load Database
using(IDbConnection conn = new SQLiteConnection(LoadConnectionString()))
{
// Open Connection To Database
conn.Open();
// Create a Command Object For Use With Connection
SQLiteCommand SqliteController = (SQLiteCommand)conn.CreateCommand();
// Assign SQL Query
SqliteController.CommandText = CreateStatement;
// Execute Query
SqliteController.ExecuteNonQuery();
// Close The Connection
conn.Close();
}
}
public static List<Posts> ReadTable()
{
// Load Database
using(IDbConnection conn = new SQLiteConnection(LoadConnectionString()))
{
// SQL Query That Reads Everything From The Table "posts"
var sql = "SELECT * FROM posts";
// Execute Query and Convert it To Type List
var output = conn.Query<Posts>(sql).ToList();
// Return The List Of "posts" Objects
return output;
}
}
public static void InsertRecords(List<String>InsertStatements)
{
// Load Database
using (IDbConnection conn = new SQLiteConnection(LoadConnectionString()))
{
// Open Connection To Database
conn.Open();
// Create a Command Object For Use With Connection
SQLiteCommand SqliteController = (SQLiteCommand)conn.CreateCommand();
// Loop Through Every Insert Statement
foreach (String statement in InsertStatements)
{
// Current Query
SqliteController.CommandText = statement;
// Execute The SQL Query
SqliteController.ExecuteNonQuery();
}
// Close The Connection
conn.Close();
}
}
private static String LoadConnectionString()
{
return "Data Source=.\\codegitz.db;Version=3;";
}
}
}