How to Use the SQL Helper Class to Create Web APIs9 min read

The SQL Helper class is used in the Data Access Layer, which interacts with a database with the help of connection strings provided. It contains several methods, as shown below. And it improves the performance for the Business Layer and Data Access Layer.

  •  ExecuteNonQuery 
  •  ExecuteDataset
  •  ExecuteDataTable 
  •  ExecuteReader 
  •  ExcuteScalar 

ASP.NET Web API

The ASP.NET Web API is a framework for building Web APIs on the top on the .NET framework, which makes it easy to build HTTP services for a range of clients, including mobile devices, browsers, and desktop applications.

Web API is similar to the ASP.NET MVC, so it contains all MVC features:

  • Model.
  • Controller.
  • Routing.
  • Model binding.
  • Filter.
  • Dependency injections.

Create Web API

Open Visual Studio 2017.

ASP.NET Web API

Go to the New menu and create a new project.

ASP.NET Web API

In the New Project menu, select the ASP.NET Web Application on Framework 4.6. Enter the name of the project in the Solution name text box and click the OK button.

ASP.NET Web API

Once the project is created, add a new API in the Controllers folder. Right-click on Controllers > Add Controller. Now, add a scaffold and create an API Controller named MasterApiController.

ASP.NET Web API

Helper Class

Create a new folder called Helper in Solution Explorer. Then, paste the below code in your class file.

ASP.NET Web API

Follow the connection string name in the web.config file.

Write Web.Config File

I am accessing my local database in the web.config file.

Create one more data convert class using using Reflection, like below:

If you want to know what Reflection is, see my blog here.

Create a new database, table, and producer. Just use the below query.

Simply insert some employee details in this table:

ASP.NET Web API

Create an EmployeeVM model class and convert as a list.

Create a SQL Data Access class MasterImplementation in the Implementation folder.

Create an HTML file and set this as the start page.

Once running the application, the Web API REST services are ready for consuming.

ASP.NET Web API

You can easily check the custom HTTP request using an advanced REST client. After downloading, open it from your Chrome Apps List.

ASP.NET Web API

Enter the correct URL and select the right HTTP verbs on the header.

ASP.NET Web API

Once you click Send, it should hit the Web API Service.

ASP.NET Web API

Finally, the Web API is working perfectly. You get your result in JSON format and RESTful connection.

Conclusion

In this article, we have seen the technique of using the SQL Helper class in building Web APIs. If you have any queries, please tell me through the comments section.

Happy Coding!

Author Profile

Thiruppathi Rengasamy
I 'm a .Net Developer & Blogger. And also I'm working as senior software engineer in Microsoft Technologies for 5+ Years.I would like to spend my precious time in writing articles, research in .Net technologies and to learn new technologies.