NYP-2 2016 6. Hafta Uygulamaları
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 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace alisverissepeti { public partial class Form1 : Form { DataTable dt1, dt2; MySqlDataAdapter da1, da2; public Form1() { InitializeComponent(); rbalis.Checked = true; MySqlConnectionStringBuilder bag = new MySqlConnectionStringBuilder(); bag.Server = "localhost"; bag.UserID = "root"; bag.Password = "123456"; bag.Database = "hafta6_16_2"; MySqlConnection conn = new MySqlConnection(bag.ToString()); // urun tablosu sorgulari MySqlCommand cmd_urun_sel = new MySqlCommand("select id, urunadi, alisfiyati, satisfiyati from urun", conn); da1 = new MySqlDataAdapter(); da1.SelectCommand = cmd_urun_sel; // sepet tablosu sorgulari MySqlCommand cmd_sepet_sel = new MySqlCommand("select id,urunid,miktar,tip from sepet", conn); MySqlCommand cmd_sepet_ins = new MySqlCommand("insert into sepet(urunid,miktar,tip) values(@urunid,@miktar,@tip)", conn); cmd_sepet_ins.Parameters.Add("@urunid", MySqlDbType.Int32, 11, "urunid"); cmd_sepet_ins.Parameters.Add("@miktar", MySqlDbType.Int32, 11, "miktar"); cmd_sepet_ins.Parameters.Add("@tip", MySqlDbType.Int32, 11, "tip"); MySqlCommand cmd_sepet_del = new MySqlCommand("delete from sepet where id=@id", conn); cmd_sepet_del.Parameters.Add("@id", MySqlDbType.Int32, 11, "id"); da2 = new MySqlDataAdapter(); da2.SelectCommand = cmd_sepet_sel; da2.InsertCommand = cmd_sepet_ins; da2.DeleteCommand = cmd_sepet_del; urungetir(); sepetgetir(); } private void urungetir() { curun.Items.Clear(); try { dt1 = new DataTable(); da1.Fill(dt1); int satirsay = dt1.Rows.Count; for (int i = 0; i < satirsay; i++) curun.Items.Add(dt1.Rows[i]["urunadi"]); curun.Text = "Seçiniz"; } catch { curun.Text = "Hata"; } } private void sepetgetir() { double tutar=0; lurun.Items.Clear(); lmiktar.Items.Clear(); lislem.Items.Clear(); ltutar.Items.Clear(); try { dt2 = new DataTable(); da2.Fill(dt2); int satirsay = dt2.Rows.Count; for (int i = 0; i < satirsay; i++) { int urunid= (int)dt2.Rows[i]["urunid"]; int miktar = (int)dt2.Rows[i]["miktar"]; int tip = (int)dt2.Rows[i]["tip"]; double uruntutar = 0; DataRow dr = urunbul(urunid); if (dr != null) { lurun.Items.Add(dr["urunadi"]); lmiktar.Items.Add(miktar); if (tip == 0) { lislem.Items.Add("Alış"); uruntutar = miktar * (double)dr["alisfiyati"]; tutar -= uruntutar; } else { lislem.Items.Add("Satış"); uruntutar = miktar * (double)dr["satisfiyati"]; tutar += uruntutar; } ltutar.Items.Add(uruntutar); } } curun.Text = "Seçiniz"; } catch { curun.Text = "Hata"; } latutar.Text = tutar + " TL"; } private DataRow urunbul(int id) { int satirsay = dt1.Rows.Count; for(int i=0;i<satirsay;i++) { if ((int)dt1.Rows[i]["id"] == id) return dt1.Rows[i]; } return null; } private DataRow urunbul1(int id) { try { DataRow[] dr = dt1.Select("id=" + id); // datatable icerisinde yer alan bir kolon ile ilgili filtre if (dr.Length > 0) return dr[0]; } catch { } return null; } private void bekle_Click(object sender, EventArgs e) { int miktar; int secilen = curun.SelectedIndex; bool kontrol = Int32.TryParse(tmiktar.Text, out miktar); if (secilen > -1 && kontrol) { try { DataRow dr = dt2.NewRow(); dr["urunid"] = dt1.Rows[secilen]["id"];// ekleme siralari ayni oldugu icin dr["miktar"] = miktar; if (rbalis.Checked) dr["tip"] = 0; else dr["tip"] = 1; dt2.Rows.Add(dr); da2.Update(dt2); curun.Text = "Seçiniz"; rbalis.Checked = true; tmiktar.Clear(); } catch { } sepetgetir(); // liste guncelle } } private void bsil_Click(object sender, EventArgs e) { int secilen = lurun.SelectedIndex; if (secilen > -1) { try { dt2.Rows[secilen].Delete(); da2.Update(dt2); } catch { } sepetgetir(); // listeyi guncelle } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
CREATE SCHEMA `hafta6_16_2` DEFAULT CHARACTER SET utf8 COLLATE utf8_turkish_ci ; CREATE TABLE `hafta6_16_2`.`urun` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '', `urunadi` VARCHAR(45) NULL COMMENT '', `alisfiyati` DOUBLE NULL COMMENT '', `satisfiyati` DOUBLE NULL COMMENT '', PRIMARY KEY (`id`) COMMENT ''); CREATE TABLE `hafta6_16_2`.`sepet` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '', `urunid` INT NULL COMMENT '', `miktar` INT NULL COMMENT '', `tip` INT NULL COMMENT '', PRIMARY KEY (`id`) COMMENT ''); INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Masa', '25', '45'); INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Sandalye', '15', '20'); INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Sehpa', '5', '15'); INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('TV', '1200', '1350'); INSERT INTO `hafta6_16_2`.`urun` (`urunadi`, `alisfiyati`, `satisfiyati`) VALUES ('Halı', '450', '600'); |
NYP-2 2016 6. Hafta Uygulamaları
Okumaya devam et
Son Yorumlar