Notes
![]() ![]() Notes - notes.io |
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 System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
SqlConnection baglanti = new SqlConnection();
private void button1_Click(object sender, EventArgs e)
{
baglanti.ConnectionString = @"Data Source=LAB2-5SQLEXPRESS;Initial Catalog=MusteriSiparis;Integrated Security=True";
// Insert INTO'da value gireceğimiz yerlere otomatik artanları yazmıyoruz, not null olanları kesinlikle yazıyoruz, öbürleri tercihimize kalmış.
//string sorgu = $"INSERT INTO Musteri(Ad, Soyad, Sehir, Ulke, Telefon) values ('{textAd.Text}', '{textSoyAd.Text}', '{textSehir.Text}', '{textUlke.Text}', '{textNo.Text}')";
//yukaridaki sorgu SQL Injection doğurur o yüzden alttakini yapmalıyız
string sorgu = $"INSERT INTO Musteri(Ad, Soyad, Sehir, Ulke, Telefon) values (@Ad, @Soyad, @il, @memleket, @gsm)"; //parametre tanımladık
SqlCommand komut = new SqlCommand();
komut.Connection = baglanti;
komut.CommandText = sorgu;
komut.Parameters.AddWithValue("@Ad", textAd.Text); //parametrelere addwithvalue ile değer atadık
komut.Parameters.AddWithValue("@Soyad", textSoyAd.Text);
komut.Parameters.AddWithValue("@il", textSehir.Text);
komut.Parameters.AddWithValue("@memleket", textUlke.Text);
komut.Parameters.AddWithValue("@gsm", textNo.Text);
baglanti.Open();
int sonuc = komut.ExecuteNonQuery(); //select dışında bir şey işleteceksek bununla işletmeliyiz. yani executereader yapmaya kalkma, bu metot bize işlem gören kayıt sayısını int olarak geri döndürür
baglanti.Close();
MessageBox.Show("Eklenen kayıt sayısı= " + sonuc);
}
private void textID_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
baglanti.ConnectionString = @"Data Source=LAB2-5SQLEXPRESS;Initial Catalog=MusteriSiparis;Integrated Security=True";
SqlCommand verigetir = new SqlCommand($"SELECT * From Musteri WHERE Id = @Id");
verigetir.Connection = baglanti;
verigetir.Parameters.AddWithValue("@Id", textID.Text);
baglanti.Open();
SqlDataReader rd = verigetir.ExecuteReader();
if (rd.Read())
{
textAd.Text = rd.GetValue(1).ToString();
textSoyAd.Text = rd.GetValue(2).ToString();
textSehir.Text = rd.GetValue(3).ToString();
textUlke.Text = rd.GetValue(4).ToString();
textNo.Text = rd.GetValue(5).ToString();
}
rd.Close();
baglanti.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
baglanti.ConnectionString = @"Data Source=LAB2-5SQLEXPRESS;Initial Catalog=MusteriSiparis;Integrated Security=True";
string sorgu2 = $"UPDATE Musteri SET Ad=@Ad, Soyad=@Soyad, Sehir=@il, Ulke=@memleket, Telefon=@gsm WHERE Id =@Id";
SqlCommand komut2 = new SqlCommand();
komut2.Connection = baglanti;
komut2.CommandText = sorgu2;
komut2.Parameters.AddWithValue("@Id", textID.Text);
komut2.Parameters.AddWithValue("@Ad", textAd.Text);
komut2.Parameters.AddWithValue("@Soyad", textSoyAd.Text);
komut2.Parameters.AddWithValue("@il", textSehir.Text);
komut2.Parameters.AddWithValue("@memleket", textUlke.Text);
komut2.Parameters.AddWithValue("@gsm", textNo.Text);
baglanti.Open();
int sonuc = komut2.ExecuteNonQuery();
baglanti.Close();
MessageBox.Show("Güncellenen kayıt sayısı= " + sonuc);
}
private void button3_Click(object sender, EventArgs e)
{
baglanti.ConnectionString = @"Data Source=LAB2-5SQLEXPRESS;Initial Catalog=MusteriSiparis;Integrated Security=True";
string sorgu3 = $"DELETE FROM Musteri WHERE Id=@Id";
SqlCommand komut3 = new SqlCommand();
komut3.Connection = baglanti;
komut3.CommandText = sorgu3;
komut3.Parameters.AddWithValue("@Id", textID.Text);
baglanti.Open();
int sonuc = komut3.ExecuteNonQuery(); // buraya bakicam hata veriyor sebebine bakicam
baglanti.Close();
MessageBox.Show("Silinen kayıt sayısı= " + sonuc);
}
DataSet ds = new DataSet(); //birçok datatable'ı içinde barındırıyor bu class
private void button4_Click(object sender, EventArgs e)
{
baglanti.ConnectionString = @"Data Source=LAB2-5SQLEXPRESS;Initial Catalog=MusteriSiparis;Integrated Security=True";
SqlDataAdapter adp = new SqlDataAdapter();
SqlCommand kmt = new SqlCommand();
kmt.CommandText = "SELECT * FROM Musteri";
kmt.Connection = baglanti;
adp.SelectCommand = kmt; //bizim select komutunu adapterin selectine ekledik
baglanti.Open();
adp.Fill(ds); // isteseydim "adp.Fill(ds, "tunanintablosu");" olarak yazip tabloya isim verebilirdim
dataGridView1.DataSource = ds.Tables[0]; // içindeki ilk datatable'ı çağırdım ya da ds.Tables["tunanintablosu"]; yapabilirdim
baglanti.Close();
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show(ds.Tables[0].Rows.Count.ToString() + " adet müşteriniz var efendimissss"); //kac tane musteri var ona baktik
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team