Welcome to FoxLearn
In this video, I'll show you how to fetch data from date to date in SQL database using C# Windows Forms Application
After you finish creating a new Windows Forms project, you need to open your form designer
and then drag the Button, DateTimePicker, Label and DataGridView controls from the Visual Studio toolbox into your form designer
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
You can design a simple user interface that allows you to filter data from a date range, then display the data into the DataGridView
After you finish designing the form. You need to open the app.config file, and then add a connection string to the app.config file
You can easily change the connection string without modifying your source code
And don't forget to add 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 need to 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 data returned from the SQL query
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 write a simple query that combines the orders and customers table, and then filter the order data by order date
After successfully executing the query, you need to copy the query into your code
Thank you for watching this video and don't forget to subscribe to my channel