C# Linq ile Kullanıcı Girişi Formu

sdkbyrm

webmasterfrm
Üyelik Tarihi
23 Aralık 2020
Mesajlar
813
Beğeniler
1
Ticaret: 0 / 0 / 0
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.



login1 login2

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

login3

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

login4

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.

login6

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

login7

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

login8

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;
}

}
Metodumuzu oluşturduktan sonra Buttona çift tıklayarak aşağıdaki kodları yazıyoruz.


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ş");
}
}
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.

login9

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;
}

}
}
}
 
Üst