Introduction
This article is about how to fetch data from Microsoft SQL to C#. Microsoft SQL has basic functionality to retrieve data as required by other applications. It can be used as a backend to create an application, and the steps desired to fetch data are as follows.
Step 1
Open Microsoft SQL and create a database (here I created a database named MYDB1) after creating a table and putting records in it.
Step 2
Open Visual Studio and create a new Windows form application.
Step 3
Create a form using buttons (insert ,search,update,delete and navigation), textbox, labels, and datagridview.
Step 4
After creating the form, connect the database to the form. For that right click on the form and it opens Form1.cs. It contains the namespace System.Data.SqlClient which applies SQL connection, SQLdataadopter, SQL command, and dataTable.for SQL data connection. Write the connection string given below and write a select query for the SQL command.
Step 5
Write code for search, insert, delete, and update buttons.
SEARCH BUTTON
For searching the data first open the connection then write the select query and uses the SQL command as it sends the query to the database and SQL DataReader as it reads the data from the SQL database then assigns the column values to textboxes and last creates a message box of "NO DATA FOUND" and closes the connection.
INSERT BUTTON
For inserting the data connect the database with a connection string and then write the insert query and create a method named executemyquery in this method first open the connection and write a command and create message boxes of data inserted successfully and data not inserted successfully and finally close the connection.
DELETE BUTTON
For deleting the data first write the delete query and create a method named delete data. In this method first open the connection and write the command and create message boxes of data deleted successfully and data not deleted successfully and lastly close the connection.
UPDATE BUTTON
For updating the data first write the update query and create a method named update query. In this method first open the connection and write the command and create message boxes of data updated successfully and data not updated successfully and lastly close the connection.
NAVIGATION BUTTON
- First navigation
- Next navigation
- Previous navigation
- Last navigation
For the navigation first, last, next, and the previous button above the code is written. In this code, create a method named show data that includes text boxes that read the data from rows and convert it to string datatype and for the next button, int variable name position which applies operator increment and position variable should be greater than the rows of the table. If all the records have been shown then show the message box of no records and for the previous button use variable name position and apply operator decrement. For that, the variable should be greater than and equal to zero and if all the records have been shown then show the message box of no records and for the last navigation variable is equal to one less than the number of counts in the table.
Conclusion
In this article, we have created a form that includes an insert button, an update button, a delete button, a search button, and a navigation button.