-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBController.cs
43 lines (41 loc) · 1.75 KB
/
DBController.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
using System.Data.SQLite;
using System.IO;
namespace s2._3
{
class DBController
{
SQLiteConnection DBConnection;
string Path;
public DBController(string path)
{
Path = path;
if (!File.Exists(Path))
SQLiteConnection.CreateFile(Path);
DBConnection = new SQLiteConnection(@"DataSource=" + Path + "; Version=3;");
DBConnection.Open();
string commandText = "CREATE TABLE IF NOT EXISTS [articles] ( " +
"[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"[head] TEXT NOT NULL, " +
"[body] TEXT NOT NULL, " +
"[date] TEXT NOT NULL, " +
"[theme] TEXT NOT NULL)";
ExecuteCommand(commandText);
}
private void ExecuteCommand(string commandText = null, SQLiteCommand sQLiteCommand = null)
{
if (sQLiteCommand == null)
sQLiteCommand = new SQLiteCommand(commandText, DBConnection);
sQLiteCommand.ExecuteNonQuery();
}
public void ArticleInsert(string head, string body, string date, string theme)
{
string commandText = "INSERT INTO [articles] ([head], [body], [date], [theme]) VALUES (@head, @body, @date, @theme)";
var command = new SQLiteCommand(commandText, DBConnection);
command.Parameters.AddWithValue("@head", head);
command.Parameters.AddWithValue("@body", body);
command.Parameters.AddWithValue("@date", date);
command.Parameters.AddWithValue("@theme", theme);
ExecuteCommand(commandText, command);
}
}
}