Bu yazımızda Linq sorgusu kullanarak C# Windows Formda basit bir Kullanıcı Girişi (Login Form) oluşturacağız.
SQL Server‘ da veri tabanımızı aşağıdaki şekilde oluşturuyoruz ve oluşturduktan sonra deneme amaçlı birkaç kullanıcı kaydediyoruz.
Daha sonra form tasarımını yapıyoruz.
Kullanıcı ve parola doğru girilmişse 2. Form’ açılması için “Solution Explorer” penceresinden Projemize yeni 2. bir form (Form2) ekliyoruz.
Daha sonra yine Solution Explorer penceresinde projemiz üzerinde sağ tıklayarak New Item diyoruz ve gelen pencerede Data bölümünden LINQ to SQL Classes seçip Add butonuna tıklıyoruz.
Daha sonra Server Explorer penceresinden aşağıdaki resimde görüldüğü gibi tablomuzu sağ tarafa sürüklüyoruz.
Bu işlemden sonra tablomuzun eklendiğini göreceğiz.
Kodlarımızı yazmaya başlıyoruz.
Geriye bool döndüren “kullanicidogrula” isimli bir metot oluşturarak gönderilen parametreye göre true yada false değeri alacağız.
Metodumuzu oluşturduktan sonra Buttona çift tıklayarak aşağıdaki kodları yazıyoruz.
Programımızı artık çalıştırabiliriz. Kullanıcı adı ve şifre doğru girildiğinde Form2 açılacak, yanlış girildiğinde ise “Hatalı giriş” mesajı verilecektir.
Kodların tamamı aşağıdaki gibi olacaktır.
SQL Server‘ da veri tabanımızı aşağıdaki şekilde oluşturuyoruz ve oluşturduktan sonra deneme amaçlı birkaç kullanıcı kaydediyoruz.


Daha sonra form tasarımını yapıyoruz.

Kullanıcı ve parola doğru girilmişse 2. Form’ açılması için “Solution Explorer” penceresinden Projemize yeni 2. bir form (Form2) ekliyoruz.

Daha sonra yine Solution Explorer penceresinde projemiz üzerinde sağ tıklayarak New Item diyoruz ve gelen pencerede Data bölümünden LINQ to SQL Classes seçip Add butonuna tıklıyoruz.

Daha sonra Server Explorer penceresinden aşağıdaki resimde görüldüğü gibi tablomuzu sağ tarafa sürüklüyoruz.

Bu işlemden sonra tablomuzun eklendiğini göreceğiz.

Kodlarımızı yazmaya başlıyoruz.
Geriye bool döndüren “kullanicidogrula” isimli bir metot oluşturarak gönderilen parametreye göre true yada false değeri alacağız.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private bool kullanicidogrula(string kAdi,string kParola) { DataClasses1DataContext context = new DataClasses1DataContext(); var sorgu=from p in context.kullanicis where p.kullaniciAdi == kAdi && p.parola == kParola select p; if (sorgu.Any()) { return true; } else { return false; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void button1_Click(object sender, EventArgs e) { if(kullanicidogrula(textBox1.Text,textBox2.Text)) { Form2 f2 = new Form2(); f2.Show(); } else { MessageBox.Show("Hatalı giriş"); } } |

Kodların tamamı aşağıdaki gibi olacaktır.
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 | 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; namespace loginForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if(kullanicidogrula(textBox1.Text,textBox2.Text)) { Form2 f2 = new Form2(); f2.Show(); } else { MessageBox.Show("Hatalı giriş"); } } private bool kullanicidogrula(string kAdi,string kParola) { DataClasses1DataContext context = new DataClasses1DataContext(); var sorgu=from p in context.kullanicis where p.kullaniciAdi == kAdi && p.parola == kParola select p; if (sorgu.Any()) { return true; } else { return false; } } } } |