In this article, we examine Select operations using the traditional Micro-ORM approach with Insight.Database.
![Insight.Database - .NET Micro ORM]()
Please check my previous articles
Let's continue with this tutorial now.
Prerequisites
- Install the SDK for.NET Core 5.0 or later.
- Install SQL Server Management Studio with Visual Studio 2019.
- Database Server 2008 R2 or Later
- After creating a new database, run the SQL scripts provided below.
DROP PROCEDURE IF EXISTS [dbo].[P_GetUserEmailAddress]
GO
DROP PROCEDURE IF EXISTS [dbo].[P_GetAllUsers]
GO
DROP TABLE IF EXISTS [dbo].[User]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[UserId] [bigint] IDENTITY(100,1) NOT NULL,
[FirstName] [nvarchar](300) NULL,
[LastName] [nvarchar](300) NULL,
[EmailAddress] [nvarchar](350) NULL,
[Country] [nvarchar](350) NULL,
CONSTRAINT [PK_dbo.User] PRIMARY KEY CLUSTERED
(
[UserId] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[User] ON
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (109, N'Colleen', N'Kessler', N'samara_corwin@nolanschuster.name', N'Marshall Islands')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (108, N'Graciela', N'Keeling', N'nakia_buckridge@goldner.biz', N'Cameroon')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (107, N'Rosie', N'Mertz', N'quinn_altenwerth@effertz.us', N'United States of America')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (106, N'Amelia', N'Weimann', N'braxton@sauerlittel.name', N'Saint Helena')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (105, N'Rosalyn', N'Hammes', N'magdalena.jones@hirthe.biz', N'India')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (104, N'Reagan', N'Schneider', N'earl@jones.us', N'Saint Helena')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (103, N'Anderson', N'Balistreri', N'ismael@considine.name', N'Svalbard & Jan Mayen Islands')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (102, N'Maegan', N'Marks', N'maurine.boehm@halvorson.ca', N'Moldova')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (101, N'Alverta', N'Dibbert', N'bud@streich.com', N'Saint Pierre and Miquelon')
GO
INSERT [dbo].[User] ([UserId], [FirstName], [LastName], [EmailAddress], [Country]) VALUES (100, N'Khalil', N'Fay', N'boris_koch@bailey.info', N'Lithuania')
GO
SET IDENTITY_INSERT [dbo].[User] OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[P_GetAllUsers]
as
BEGIN
SELECT * FROM [dbo].[User]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[P_GetUserEmailAddress]
@UserId BIGINT
as
BEGIN
SELECT [EmailAddress] FROM [dbo].[User]
WHERE UserId = @UserId
END
GO
Added User Repository with Interface and Concrete with DI
IUserRepository
![]()
UserRepository with SQL Connection
![]()
Registering Repository Dependencies in StartUp
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Resolve Repository Dependencies
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Selecting Records
1. Execute Scalar
ExecuteScalar and ExecuteScalarSql are supported by Insight. These techniques yield the first column of the set's first row.
Execute Scalar is used for calling Stored Procedures which returns single records. Create an SP which returns a single record as given below.
To obtain an email address, add GetUserEmailAddress methods to the Repository Interface, Concrete, and Controller.
Swagger output to get Email Address by User Id
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Execute Scalar SQL is used for Select Statement which returns single records.
Add GetUserEmailAddress_V1 methods to the Repository Interface, Concrete, and Controller to acquire a user's email address.
Here is the Swagger output for getting an email address from a User Id
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Execute Query
Wiki Document Link
A row or more rows of a table are returned using Query and QuerySql methods.
1. Query
Query is used for calling Stored Procedures which is a row or multiple rows, Create an SP as given below.
To get all users data, add GetAllUsers methods to the Repository Interface, Concrete, and Controller.
Swagger output to get All User Information
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
2. QuerySQL
QuerySQL is used for direct SQL Select statements to get a row or multiple rows.
Add GetAllUsers_V1 methods to the Repository Interface, Concrete, and Controller to acquire all user details.
Swagger output: Get All User's Data
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Added Unit Test for UserController
Test Results
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
Project structure of this article,
![Insight.Database, .NET Micro ORM - Executing and carrying out SELECT SQL commands]()
In the next article. we will discuss on implementing Insert, Update, Delete, and other features.