· 7 years ago · Jan 16, 2019, 02:04 AM
1using System;
2using System.Data.SQLite;
3using System.IO;
4
5
6namespace csharpsqlite
7{
8 class Program
9 {
10 private const string _db = "hf.sqlite";
11 private static string _connectionString = $"Data Source={_db};Version=3;";
12 private const string _createHfTable = "create table if not exists hf(forumname varchar(100),forumlink varchar(100))";
13 private const string _insertData = "insert into hf (forumname, forumlink) values ('hackforums', 'www.hackforums.net')";
14 private const string _readData = "select * from hf";
15
16 static void Main(string[] args)
17 {
18 CreateSQLiteDB(_db);
19 CreateTable();
20 InsertData();
21 ReadData();
22
23 Console.ReadLine();
24 }
25
26 private static void CreateSQLiteDB(string db)
27 {
28 if(!File.Exists(db))
29 {
30 Console.WriteLine($"{db} is created");
31 SQLiteConnection.CreateFile(db);
32 }
33 else
34 {
35 Console.WriteLine($"{db} exists");
36 }
37 }
38
39 private static void CreateTable()
40 {
41 using (var sqlite = new SQLiteConnection(_connectionString))
42 {
43 sqlite.Open();
44 SQLiteCommand command = new SQLiteCommand(_createHfTable, sqlite);
45 command.ExecuteNonQuery();
46 }
47 }
48
49 private static void InsertData()
50 {
51 using (var sqlite = new SQLiteConnection(_connectionString))
52 {
53 sqlite.Open();
54 SQLiteCommand command = new SQLiteCommand(_insertData, sqlite);
55 command.ExecuteNonQuery();
56 }
57 }
58
59 private static void ReadData()
60 {
61 using (var sqlite = new SQLiteConnection(_connectionString))
62 {
63 sqlite.Open();
64 SQLiteCommand command = new SQLiteCommand(_readData, sqlite);
65 SQLiteDataReader reader = command.ExecuteReader();
66 while (reader.Read())
67 Console.WriteLine(String.Format("Forum name:{0} Forum link:{1}", reader["forumname"], reader["forumlink"]));
68 }
69 }
70 }
71}