-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForm2.cs
103 lines (83 loc) · 2.95 KB
/
Form2.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
using System;
using System.Data.SQLite;
using System.Windows.Forms;
namespace Code_Snippet_Manager
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
#region SAVE button
private void button1_Click(object sender, EventArgs e)
{
//check whether the title is empty or not
if (String.IsNullOrEmpty(snippet_title_txt.Text))
{
MessageBox.Show("Enter Title!! ");
return;
}
SQLiteConnection Conn;
SQLiteCommand cmd;
//check whether the database already exist in the target machine
if (System.IO.File.Exists("Snippetdb.sqlite") == false)
{
SQLiteConnection.CreateFile("Snippetdb.sqlite");
Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
//connect to database
Conn.Open();
string query = "create table snippet (Name TEXT, Code TEXT)";
cmd = new SQLiteCommand(query, Conn);
cmd.ExecuteNonQuery();
//close conection
Conn.Close();
}
Conn = new SQLiteConnection("Data Source=Snippetdb.sqlite;Version=3;");
//check if there exist any snippet with the same title
cmd = new SQLiteCommand("SELECT Name FROM snippet WHERE (Name=@Name)");
cmd.Parameters.AddWithValue("@Name", snippet_title_txt.Text);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
Conn.Open();
cmd.ExecuteNonQuery();
SQLiteDataReader reader = cmd.ExecuteReader();
int count = 0;
while (reader.Read())
{
++count;
}
Conn.Close();
if (count == 1)
{
MessageBox.Show("Snippet with same Title already exist!!");
return;
}
cmd = new SQLiteCommand("INSERT INTO snippet (Name, Code) VALUES (@Name, @Code)");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = Conn;
cmd.Parameters.AddWithValue("@Name", snippet_title_txt.Text);
cmd.Parameters.AddWithValue("@Code", code_richtxt.Text);
Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();
}
#endregion
//home button
private void button2_Click(object sender, EventArgs e)
{
this.Dispose();
}
#region NEW button
private void button1_Click_1(object sender, EventArgs e)
{
snippet_title_txt.Clear();
code_richtxt.Clear();
}
#endregion
private void button2_Click_1(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
}
}
}