In this article, we will make a Sql connection using Entity Framework method using WPF Form.
We will perform Select, Add, Update and Delete operations in the project.
Let’s start building sql database design, wpf form design and entity framework connection step by step.
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
Step 11:
Step 12:
XAML Code & Design:
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 |
<Window x:Class="Wpf_Entity_Examples.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Wpf_Entity_Examples" mc:Ignorable="d" Title="www.csharp-console-examples.com" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <DataGrid Name="dgogrenci" HorizontalAlignment="Left" AutoGenerateColumns="False" Height="185" Margin="22,37,0,0" VerticalAlignment="Top" Width="246"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding ID}" Header="ID" Width="*"/> <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" Width="3*"/> <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" Width="3*" /> </DataGrid.Columns> </DataGrid> <TextBox Name="txtId" HorizontalAlignment="Left" Height="23" Margin="371,61,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> <TextBox Name="txtFirst" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Margin="371,89,0,0"/> <TextBox Name="txtLast" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Margin="371,117,0,0"/> <Button Content="Insert" HorizontalAlignment="Left" Margin="371,145,0,0" VerticalAlignment="Top" Width="120" Height="36" Click="Button_Click"/> <Button Content="Delete" HorizontalAlignment="Left" Height="36" Margin="371,186,0,0" VerticalAlignment="Top" Width="120" Click="Button_Click_1"/> <Button Content="Update" HorizontalAlignment="Left" Height="39" Margin="371,227,0,0" VerticalAlignment="Top" Width="120" Click="Button_Click_2"/> <Label x:Name="label" Content="ID" HorizontalAlignment="Left" Margin="303,61,0,0" VerticalAlignment="Top"/> <Label x:Name="label1" Content="First Name" HorizontalAlignment="Left" Margin="300,88,0,0" VerticalAlignment="Top"/> <Label x:Name="label2" Content="Last Name" HorizontalAlignment="Left" Margin="302,117,0,0" VerticalAlignment="Top"/> </Grid> </Window> |
C# Code:
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Wpf_Entity_Examples { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { dbSchoolEntities db; public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Student student = new Student(); student.FirstName = txtFirst.Text; student.LastName = txtLast.Text; db.Students.Add(student); db.SaveChanges(); dgogrenci.ItemsSource = db.Students.ToList(); } private void Button_Click_1(object sender, RoutedEventArgs e) { int num = Convert.ToInt32(txtId.Text); var dRow = db.Students.Where(w => w.ID == num).FirstOrDefault(); db.Students.Remove(dRow); db.SaveChanges(); dgogrenci.ItemsSource = db.Students.ToList(); } private void Button_Click_2(object sender, RoutedEventArgs e) { int num = Convert.ToInt32(txtId.Text); var uRow = db.Students.Where(w => w.ID == num).FirstOrDefault(); uRow.FirstName = txtFirst.Text; uRow.LastName = txtLast.Text; db.SaveChanges(); dgogrenci.ItemsSource = db.Students.ToList(); } private void Window_Loaded(object sender, RoutedEventArgs e) { db = new dbSchoolEntities(); dgogrenci.ItemsSource = db.Students.ToList(); } } } |
Output: