-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form2.cs
305 lines (237 loc) · 12.3 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace hospitaldb
{
public partial class Form2 : Form
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hasta_bilgisi.accdb"); // baglanti yol
int selectedHastaID; // form1den gelecek hasta idsi
private string selectedHastaIsim;
public Form2(int hastaID, string hastaIsim)
{
InitializeComponent();
selectedHastaID = hastaID;
selectedHastaIsim = hastaIsim;
}
private void Form2_Load(object sender, EventArgs e) //Form2 Load
{
// Form1'den gelen hasta ismini TextBox4 içine yerleştir
textBox4.Text = selectedHastaIsim;
conn.Open(); // Bağlantı objesini aç
// SQL sorgusu Access'ten alındı
string query = "SELECT tbl_Randevu.randevu_id, tbl_Hasta.isim, tbl_Randevu.randevu_tarihi, tbl_Randevu.teşhis, tbl_Randevu.tedavi, tbl_İlaç.ilaç_adı FROM tbl_İlaç INNER JOIN (tbl_Hasta INNER JOIN tbl_Randevu ON tbl_Hasta.hasta_id = tbl_Randevu.randevu_hasta_id) ON tbl_İlaç.ilaç_id = tbl_Randevu.verilen_ilaç_id; "; //sorgu yanlis randevu idye gore alinmasi gerekiyor
OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
da.SelectCommand.Parameters.AddWithValue("@hastaID", selectedHastaID);
// DataTable doldur
DataTable dt = new DataTable();
da.Fill(dt);
// DataGridView'in sourcesini DataTable'e bağla
dataGridView1.DataSource = dt;
// ComboBox için SQL sorgusu
string comboBoxQuery = "SELECT ilaç_id, ilaç_adı FROM tbl_İlaç";
OleDbDataAdapter comboBoxDa = new OleDbDataAdapter(comboBoxQuery, conn);
DataTable comboBoxDt = new DataTable();
comboBoxDa.Fill(comboBoxDt);
// ComboBox'ı doldur
comboBox1.DisplayMember = "ilaç_adı";
comboBox1.ValueMember = "ilaç_id";
comboBox1.DataSource = comboBoxDt;
conn.Close(); // Bağlantıyı kapat
}
public void button1_Click(object sender, EventArgs e) // Yeni
{
// TextBox'lari temizle
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
// MaskedTextBox'i temizle
maskedTextBox1.Clear();
// DateTimePicker'i bugunun tarihine ayarla
dateTimePicker1.Value = DateTime.Now;
// ComboBox'i temizle
comboBox1.SelectedIndex = -1;
comboBox1.Text = null;
}
private void button2_Click(object sender, EventArgs e) // Ekle
{
// Bağlantıyı aç
conn.Open();
// INSERT sorgusu için parametreler
string insertQuery = "INSERT INTO tbl_Randevu (randevu_tarihi, teşhis, tedavi, verilen_ilaç_id, randevu_hasta_id) " +
"VALUES (@randevu_tarihi, @teşhis, @tedavi, @verilen_ilaç_id, @randevu_hasta_id)";
using (OleDbCommand cmd = new OleDbCommand(insertQuery, conn))
{
// Gerekli alanların kontrolü
if (string.IsNullOrEmpty(textBox3.Text) ||
string.IsNullOrEmpty(textBox2.Text) ||
comboBox1.SelectedItem == null)
{
MessageBox.Show("Lütfen tüm bilgileri doldurunuz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; // İşlemi durdur
}
// Parametreleri ekleyerek sorguya ekle
cmd.Parameters.AddWithValue("@randevu_tarihi", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@teşhis", textBox3.Text);
cmd.Parameters.AddWithValue("@tedavi", textBox2.Text);
cmd.Parameters.AddWithValue("@verilen_ilaç_id", comboBox1.SelectedValue); // Seçilen ilacın ID'sini al
cmd.Parameters.AddWithValue("@randevu_hasta_id", selectedHastaID);
// Sorguyu çalıştır
cmd.ExecuteNonQuery();
// Son eklenen randevu ID'sini alma işlemi @@IDENTITY ile değil, son eklenenin tarih ve hasta ID'sine göre yapılmalıdır.
// Başarılı ekleme mesajı göster
MessageBox.Show("Randevu başarıyla eklendi.", "Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshDataGridView();
}
// Bağlantıyı kapat
conn.Close();
}
private void button3_Click(object sender, EventArgs e) // Değiştir
{
if (dataGridView1.SelectedRows.Count > 0)
{
int selectedRowIndex = dataGridView1.SelectedRows[0].Index;
if (selectedRowIndex >= 0)
{
// DataGridView'den seçili satırdaki randevu_id değerini al
int selectedRandevuID = Convert.ToInt32(dataGridView1.Rows[selectedRowIndex].Cells["randevu_id"].Value);
// Güncelleme sorgusunu düzgün bir şekilde yazalım
string updateQuery = "UPDATE tbl_Randevu " +
"SET randevu_tarihi = @randevu_tarihi, " +
"teşhis = @teşhis, " +
"tedavi = @tedavi, " +
"verilen_ilaç_id = @verilen_ilaç_id " +
"WHERE randevu_id = @randevu_id";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(updateQuery, conn))
{
// Parametrelerin doğru şekilde eklendiğinden emin olalım
cmd.Parameters.AddWithValue("@randevu_tarihi", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@teşhis", textBox3.Text);
cmd.Parameters.AddWithValue("@tedavi", textBox2.Text);
cmd.Parameters.AddWithValue("@verilen_ilaç_id", comboBox1.SelectedValue);
cmd.Parameters.AddWithValue("@randevu_id", selectedRandevuID);
cmd.ExecuteNonQuery();
// Başarılı güncelleme mesajı göster
MessageBox.Show("Randevu başarıyla güncellendi.", "Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshDataGridView();
}
conn.Close();
}
else
{
MessageBox.Show("Geçersiz satır seçimi.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("Güncellenecek bir randevu seçiniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button4_Click(object sender, EventArgs e) // Sil
{
conn.Open();
if (dataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
int randevu_id = Convert.ToInt32(selectedRow.Cells["randevu_id"].Value); // randevu id'den alınıyor
string qdelete = "DELETE FROM tbl_Randevu WHERE randevu_id = ?";
using (OleDbCommand cmd = new OleDbCommand(qdelete, conn))
{
cmd.Parameters.AddWithValue("@randevu_id", randevu_id);
cmd.ExecuteNonQuery();
// Başarılı silme mesajı göster
MessageBox.Show("Randevu başarıyla silindi.", "Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshDataGridView(); // DataGridView'i güncelle
}
}
else
{
// Silinecek bir randevu seçilmediği durumda kullanıcıya uyarı ver
MessageBox.Show("Silinecek bir randevu seçiniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
conn.Close();
}
private void button5_Click(object sender, EventArgs e) // Hasta
{
this.Close(); //kapa
}
private void button6_Click(object sender, EventArgs e) // Cikis
{
// onay mesaj box
DialogResult result = MessageBox.Show("Randevu sekmesini kapatmak istediğinize emin misiniz?", "Çıkış", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// evetse cik
if (result == DialogResult.Yes)
{
// kapa
this.Close();
}
}
public void RefreshDataGridView() // DataGrid Yenileme Fonksiyonu
{
// Veri kaynağını temizleme
dataGridView1.DataSource = null;
// Veri tekrar doldur
OleDbDataAdapter da = new OleDbDataAdapter("SELECT tbl_Randevu.randevu_id, tbl_Hasta.isim, tbl_Randevu.randevu_tarihi, tbl_Randevu.teşhis, tbl_Randevu.tedavi, tbl_İlaç.ilaç_adı FROM tbl_İlaç INNER JOIN (tbl_Hasta INNER JOIN tbl_Randevu ON tbl_Hasta.hasta_id = tbl_Randevu.randevu_hasta_id) ON tbl_İlaç.ilaç_id = tbl_Randevu.verilen_ilaç_id; ", conn);
DataTable dt = new DataTable();
da.Fill(dt);
// DataGridView'in veri kaynağını güncelle
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// ContentDouble click ergonomik degil cell click ile degistirildi
}
private void KontrolYukle(int rowIndex)
{
DataGridViewRow selectedRow = dataGridView1.Rows[rowIndex];
// İsim bilgisini al, eğer null veya boş ise MessageBox göster ve metottan çık
if (string.IsNullOrEmpty(selectedRow.Cells["isim"].Value?.ToString()))
{
MessageBox.Show("Randevu kaydı bulunamadı", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Verileri kontrollerine aktar
textBox4.Text = selectedRow.Cells["isim"].Value.ToString();
dateTimePicker1.Value = Convert.ToDateTime(selectedRow.Cells["randevu_tarihi"].Value);
maskedTextBox1.Text = selectedRow.Cells["randevu_tarihi"].Value.ToString();
textBox3.Text = selectedRow.Cells["teşhis"].Value.ToString();
textBox2.Text = selectedRow.Cells["tedavi"].Value.ToString();
comboBox1.Text = selectedRow.Cells["ilaç_adı"].Value.ToString();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
//Yanlis Acildi kullanilmadi
}
private void button7_Click(object sender, EventArgs e) // Tumunu Listele
{
RefreshDataGridView();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT tbl_Hasta.hasta_id," + //sorgu refreshdatagridviewden alindi
" tbl_Hasta.isim, tbl_Randevu.randevu_tarihi," +
" tbl_Randevu.teşhis, tbl_Randevu.tedavi," +
" tbl_İlaç.ilaç_adı FROM tbl_İlaç INNER JOIN (tbl_Hasta INNER JOIN" +
" tbl_Randevu ON tbl_Hasta.hasta_id = tbl_Randevu.randevu_hasta_id)" +
" ON tbl_İlaç.ilaç_id = tbl_Randevu.verilen_ilaç_id WHERE isim LIKE '" + textBox1.Text + "%'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
KontrolYukle(e.RowIndex);
}
}
}