Introduction
This article describes the procedure for searching data in an existing table. We find the record from the table using a SQL query. We create a database "Demo" and table "info" in SQL Server 2012.
The following is the procedure for creating this application.
Step 1
First we create a table and insert some data into the table.
- Start SQL Server 2012.
- Select "New Query".
- Create a new database and table.
Use these commands:
- create database Demo // create the database
- use Demo // use database and create table in the "Demo" database
- create table info (ID int IDENTITY PRIMARY KEY,Name varchar(50),Appointment varchar(50),Technology varchar(50), Task varchar(MAX) )// create a new table
- Insert into info values ('A.P godse','Software Develpoer','.NET, Database','Computer Graphics') // Insert record in the table
- Insert into info values ('yashwant Kanitker','Programmer','.NET, PHP, JSON','Let us C')
- Insert into info values ('E balaguruswami','Programmer', '.NET, Javascript','Object Oriented System')
- Insert into info values ('R.S, Aggrawal','Software Developer',' Web API, MVC4','Design Analysis and Algorithm')
Step 2
Now create a Web API application.
-
Start Visual Studio 2012.
-
From the start window select "New Project".
-
In the Template Window select "Installed" -> "Visual C#" -> "Web".
-
Select "ASP.NET MVC 4 Web Application" and click on "OK".

-
From the "MVC4 Project" window select "Web API".

Step 3
Create a Model class.
-
In the "Solution Explorer".
-
Right-click on the "Model folder".
-
Select "Add" -> "Class".

-
In the Template window select "Installed" -> "Visual C#" -> "Class"

-
Click on the "OK" button.
The code of this class:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Web;
- namespace Search.Models
- {
- public class Detail
- {
- public int bID { get; set; }
- public string bName { get; set; }
- public string bAppointment { get; set; }
- public string bTechnology { get; set; }
- public string bTask { get; set; }
- }
- public class ShowDetail
- {
- public List<Detail> Search(List<string> Information)
- {
- StringBuilder Buildsql = new StringBuilder();
- Buildsql.Append("select * from [info] where ");
- foreach (string value in Information)
- {
- Buildsql.AppendFormat("([Name] like '%{0}%' or [Appointment] like '%{0}%' or[Technology] like '%{0}%' or[Task] like '%{0}%') and ", value);
- }
- string datasql = Buildsql.ToString(0, Buildsql.Length - 5);
- return QueryList(datasql);
- }
- protected List<Detail> QueryList(string text)
- {
- List<Detail> lst = new List<Detail>();
- SqlCommand cmd = GenerateSqlCommand(text);
- using (cmd.Connection)
- {
- SqlDataReader reader = cmd.ExecuteReader();
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- lst.Add(ReadValue(reader));
- }
- }
- }
- return lst;
- }
- protected SqlCommand GenerateSqlCommand(string cmdText)
- {
- SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
- SqlCommand cmd = new SqlCommand(cmdText, con);
- cmd.Connection.Open();
- return cmd;
- }
- protected Detail ReadValue(SqlDataReader reader)
- {
- Detail dt = new Detail();
- dt.bID = (int)reader["ID"];
- dt.bName = (string)reader["Name"];
- dt.bAppointment = (string)reader["Appointment"];
- dt.bTechnology = (string)reader["Technology"];
- dt.bTask = (string)reader["Task"];
- return dt;
- }
- }
- }







Misbah ShakeelPosted Feb 1, 2023, 5:58 AM
What if we search technology in postman ?