· 6 years ago · Sep 08, 2019, 05:56 PM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.Data.SQLite;
11using System.IO;
12
13namespace Aplikacija
14{
15 public partial class DodajStan : Form
16 {
17
18 public DodajStan(ThirdUserControl third)
19 {
20 InitializeComponent();
21 createTable();
22
23 }
24 //string fileName;
25
26
27 public delegate void UpdateDelegate(object sender, UpdateEventArgs args);
28 public event UpdateDelegate UpdateEventHandler;
29
30 public class UpdateEventArgs : EventArgs
31 {
32 public string Data { get; set; }
33 }
34
35 protected void insert()
36 {
37 UpdateEventArgs args = new UpdateEventArgs();
38
39 UpdateEventHandler.Invoke(this, args);
40
41 }
42 public void createTable()
43 {
44 SQLiteConnection con = new SQLiteConnection("Data Source = Account.db; Version = 3");
45 con.Open();
46
47 SQLiteCommand cmd = new SQLiteCommand();
48
49 string query = @"CREATE TABLE if not exists Stan (ID INTEGER PRIMARY KEY AUTOINCREMENT, Lokacija Text(25), BrojSoba INTGER(25), Kat INTGER(25), Cijena INTGER(25),Slika BLOB,
50 ID_ACC INTEGER, FOREIGN KEY(ID_ACC) REFERENCES 'Account'('Account_ID'))";
51 cmd.CommandText = query;
52 cmd.Connection = con;
53 cmd.ExecuteNonQuery();
54 }
55 private void dodajStanButton_Click(object sender, EventArgs e)
56 {
57
58 SQLiteConnection con = new SQLiteConnection("Data Source = Account.db; Version = 3");
59 con.Open();
60 SQLiteCommand cmd = new SQLiteCommand();
61 Login login = new Login();
62
63
64
65 try
66 {
67 byte[] imgBt = null;
68 FileStream fstream = new FileStream(this.txtPath.Text, FileMode.Open, FileAccess.Read);
69 BinaryReader br = new BinaryReader(fstream);
70 imgBt = br.ReadBytes((int)fstream.Length);
71
72
73 cmd = con.CreateCommand();
74 cmd.CommandText = "INSERT INTO Stan(Lokacija, BrojSoba, Kat, Cijena, Slika, ID_ACC) VALUES (@lokacija, @BrojSoba, @Kat, @cijena, @IMG, @ID_ACC)";
75 cmd.Parameters.AddWithValue("@lokacija", lokacijaText.Text);
76 cmd.Parameters.AddWithValue("@BrojSoba", brojSobaText.Text);
77 cmd.Parameters.AddWithValue("@Kat", katText.Text);
78 cmd.Parameters.AddWithValue("@cijena", cijenaText.Text);
79 cmd.Parameters.AddWithValue("@ID_ACC", login.id_acc);
80 Console.WriteLine(login.id_acc);
81 //cmd.Parameters["@id_acc"].Value = login.id_acc;
82 cmd.Parameters.Add(new SQLiteParameter("@IMG", imgBt));
83 //cmd.Parameters.Add("@id_acc", )
84 cmd.ExecuteNonQuery();
85
86 }
87 catch
88 {
89 MessageBox.Show("Not added", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
90 }
91 insert();
92 //string query = @"INSERT INTO Stan(Ime, Prezime, Email, Mobitel) VALUES (@ime, @prezime, @email, @mobitel)";
93 //cmd.CommandText = query;
94 //cmd.Connection = con;
95 //cmd.Parameters.Add(new SQLiteParameter("@ime", ime));
96 //cmd.Parameters.Add(new SQLiteParameter("@prezime", prezime));
97 //cmd.Parameters.Add(new SQLiteParameter("@email", email));
98 //cmd.Parameters.Add(new SQLiteParameter("@mobitel", mobitel));
99
100 //cmd.ExecuteNonQuery();
101
102 //MessageBox.Show("Dodan stan", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
103 }
104
105 private void cijenaLabel_Click(object sender, EventArgs e)
106 {
107
108 }
109
110 private void informacijeLabel_Click(object sender, EventArgs e)
111 {
112
113 }
114
115 private void browseButton_Click(object sender, EventArgs e)
116 {
117 //OpenFileDialog o = new OpenFileDialog();
118 //o.Filter = "JPG|*.jpg|PNG|*.png";
119 //if (o.ShowDialog() == DialogResult.OK)
120 //{
121 // txtPath.Text = o.FileName;
122 // pictureBox1.Image = new Bitmap(o.FileName);
123 //}
124 }
125
126 private void saveButton_Click(object sender, EventArgs e)
127 {
128 //try
129 //{
130 // SQLiteConnection con = new SQLiteConnection("Data Source = Account.db; Version = 3");
131 // con.Open();
132 // string imgpath = txtPath.Text;
133 // FileStream fs;
134 // fs = new FileStream(@imgpath, FileMode.Open, FileAccess.Read);
135 // byte[] picbyte = new byte[fs.Length];
136 // fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
137 // fs.Close();
138 // con.Open();
139 // string qry = "insert into Stan (IMG_PATH,IMG_PIC) values('" + txtPath.Text + "',@pic)";
140 // SQLiteParameter picpara = new SQLiteParameter();
141 // picpara.DbType = DbType.Blob;
142 // picpara.ParameterName = "pic";
143 // picpara.Value = picbyte;
144 // SQLiteCommand cmd = new SQLiteCommand(qry, con);
145 // cmd.Parameters.Add(picpara);
146 // cmd.ExecuteNonQuery();
147 // MessageBox.Show("Image Stored");
148 // cmd.Dispose();
149 // con.Close();
150
151 //}
152 //catch (Exception ex)
153 //{
154 // MessageBox.Show(ex.Message);
155 //}
156 OpenFileDialog dlg = new OpenFileDialog();
157 dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*";
158
159 if (dlg.ShowDialog() == DialogResult.OK)
160 {
161 string picPath = dlg.FileName.ToString();
162 txtPath.Text = picPath;
163 pictureBox1.ImageLocation = picPath;
164 }
165 }
166 }
167}