WPF Read a SQL Server Table into a DataGrid with Dapper


This entry is part 2 of 5 in the series Dapper

This post is about how to connect to a local SQL Server database and read a table from that database into a DataGrid that’s in a Windows Presentation Foundation (WPF) project. Here we will use Dapper. Dapper is a simple Object Relational Mapper (ORM) for .NET. Dapper competes with Entity Framework and other ORMs, but Dapper is more lightweight and has less functionality. For example, you need to write your own SQL queries with the micro-ORM Dapper.

There are YouTube videos on how to use Dapper. Here’s one called Dapper – A Simple Object Mapper for .NET – Lightning Talk – Dot Net Sheff – July 2018.

This post discusses very basic Reading of a SQL database table into a DataGrid in a Windows Presentation Foundation (WPF) project. Once you have a WPF project created, you’ll need to install Dapper, which is very easy to do.

Below is the XAML code for our new project called DapperPusherYouTube. There is just a DataGrid inside a Grid. It’s important for our DataGrid to have a Name, which is dtaGrid in this case. There is no button the user needs to click to load the data from SQL Server to the Grid – it happens automatically when the project loads. You can see that below in the code behind. Right after InitializeComponent() we have the code that reads from the database. Alternatively, we might have a button that has a click event that runs this code.

<Window x:Class="DapperPusherYouTube.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:DapperPusherYouTube"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="DapperPusherYouTube" Height="250" Width="600">
    <Grid Margin="0,0,101.6,0">
        <DataGrid Name="dtaGrid" HorizontalAlignment="Left" Height="170" 
                  Margin="47,10,0,0" VerticalAlignment="Top" Width="389"/>
    </Grid>
</Window>

Below is the code behind.

using System.Data;
using System.Windows;
using System.Data.SqlClient;
using System.Collections.Generic;
using Dapper;
using System;

namespace DapperPusherYouTube
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            try
            {
                using (IDbConnection conn = GetConnection())
                {
                    IEnumerable<Person> persons = conn.Query<Person>("SELECT * FROM Persons");
                    dtaGrid.ItemsSource = persons;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Database Error: " + ex.Message, "DapperPusherYouTube", 
                    MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        IDbConnection GetConnection()
        {
            return new SqlConnection("Server=(localdb)\\MSSQLLocalDB;Database=MyApp1;Integrated Security=true;");
        }
    }
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Wihout Dapper

Another post show us how to read a SQL Server database table into a DataGrid. The post is called WPF Read a SQL Server Table into a DataGrid.

Series Navigation<< Dapper micro-ORM IntroductionWPF Insert into SQL Server Table with Dapper >>