Reading Data Using Dapper
Learn to read data from the database using C#.
We'll cover the following
In this section, we are going to write some C# code that reads data from the database.
We are going to use the popular repository design pattern to structure our data access code. This will allow us to provide a nice, centralized abstraction of the data layer.
We are going to start by creating a data repository class that will hold all of the queries we are going to make to the data. We are going to create C# classes that hold the data we get from the database, called models.
We will implement methods for getting all the questions, getting questions from a search, getting unanswered questions, getting a single question, getting information stating whether a question exists, and getting an answer.
Creating a repository class
Let's create a class that will hold all of the methods for interacting with the database:
In “Solution Explorer,” right-click our project, select the “Add” menu, and then choose the “New Folder” option.
A new folder will be created in the solution tree. Name the folder “Data.”
Right-click the “Data” folder and select the “Add” menu. Then, choose the “Class…” option.
In the dialog box that appears, enter “DataRepository” for the name of the file and click the “Add” button.
A skeleton
DataRepository
class will be created:
Get hands-on with 1200+ tech skills courses.