Welcome to FoxLearn
In this video, I'll show you how to search data in SQL database using c-sharp Windows Forms Application
After you finish creating a new Windows Forms project, you need to open your form designer
and then drag the Button, Label, TextBox and DataGridView controls from the Visual Studio toolbox into your form designer.
Next, You can design a simple UI allows you to enter a search value into TextBox control
then search data from sql database based on the search value you entered, and display data to DataGridView
After you have finished designing the form
You need to open the app.config file, and then add a connection string to the app.config file
You should store your connection string in the app.config file to make it easy to change the connection string without modifying your source code
In this tutorial, I will use the Northwind database to play my demo, if you don't have the Northwind database in your SQL Server yet
You can watch the video I uploaded on how to download and install the Northwind database into SQL Server
And don't forget to add a reference to the System.Configuration library to your project
This library helps you to read your connection string from the app.config file
You should use try catch to catch errors if something goes wrong, then write your code in this try catch block
You can use the SqlConnection class to make a connection to your database
and don't forget to check your sql connection status
If the connection is closed you need to open it
The SqlConnection is opened and set as the Connection for the SqlCommand
You should create a new DataTable to help you store the returned data when you execute the sql query
The DataTable is a central object in the ADO.NET library
Other objects that use the DataTable include the DataSet and the DataView
Next, use the SqlCommand class to execute your SQL query
You can easily add query parameters to SqlCommand using the AddWithValue method
It represents a Transact-SQL statement or stored procedure to execute against a SQL Server database
Finally, Use the SqlDataAdapter class to read the data and fill in your DataTable object, and then bind it to your DataGridView
The SqlDataAdapter, serves as a bridge between a DataSet and SQL Server for retrieving and saving data
The SqlDataAdapter provides this bridge by mapping Fill
which changes the data in the DataSet to match the data in the data source
and Update, which changes the data in the data source to match the data in the DataSet
using the appropriate Transact-SQL statements against the data source
You can create a simple sql query that allows you to search data from the customers table by Customer ID or Contact name
For the Contact name, you can use keyword "LIKE" to search for patterns in columns of a table
As you can see, you can easily search for exact data by Customer ID
or search for customers whose contact name is the same as the search value you entered
Thank you for watching this video and don't forget to subscribe to my channel