Databases in C#
How to Create a Database
Create a new Windows Forms App (.NET Framework) project with the name Database Project 1 and save it on the desktop. Click on Project > Add New Item... from the menu as shown in the image below.
Select Service-based Database item from the Add New Item dialog box and type Employees.mdf in the Name: box as shown in the image below. Then click on Add button.
Now you can see the created database in the Solution Explorer as shown in the image below.
How to Create a Table in the Database
Right click on the database in the Solution Explorer and select Open item as shown in the image below.
The Server Explorer appears when the Open item is clicked as shown in the image below.
Right click on Tables item and select Add New Table item as shown in the image below.
A tab with the name dbo.Table [Design] appears when Add New Table item is clicked as given in the image below.
Go to the code given below in the tab.
(
[Id] INT NOT NULL PRIMARY KEY
)
Replace the word Table by tblEmployees. Then click on the Update button and what you see is shown in the image below.
Click on Update Database button. Now look at the dbo.tblEmployees [Design] tab again. You can add columns to the table and note that Id column has already been set up. The key symbol indicates that the Id is the primary key. That means, it is not possible to add duplicate items to the Id column.
Select the Id column and look at the Properties. Then expand the Identity Specification item. Set (Is Identity) item to True and you can see Identity Increment and Identity Seed items are automatically set to 1. The value of Id for the first row of the tblEmployees table is 1 since Identity Seed is 1. The value of Id for each row is incremented by 1 since Identity Increment is 1. Use the Update button to update the database.
Add Name, Title, and Salary columns to the table as shown in the image below. Note that Data Type for each column should be set to nvarchar(50), nvarchar(50), and money respectively. Use the Update button to update the database.
How to Add Data to the Table
Go to the Server Explorer and expand Tables item. Then right click on the tblEmployees item and click on Show Table Data item as shown in the image below.
The dbo.tblEmployees [Data] tab can be seen when Show Table Data item is clicked as shown in the image below.
Fill the table with the data shown in the image below. Then click on the Refresh button available on the tab.
How to Add the Database to a Project
Create a new Windows Forms App (.NET Framework) project with the name Database Project 2 and save it on the desktop. Double click on the Properties item in the Solution Explorer and what you see is shown in the image below.
Select the Employees.mdf file found in the path Desktop > Database Project 2 > Database Project 2 > Resources as given in the image below.
Then click OK button to come back to the Connection Properties dialog box. Click the OK button and then you can see what is given in the image below.
Add the second row to the table as given in the image below.
How to Create a Database Connection Class
Click on Project > Add Class... from the menu as shown in the image below.
What you can see is given in the image below when the Add button is clicked.
Edit the DatabaseConnection class as given in the image below.
How to Use the Database Connection Class
Go to the Form1.cs [Design] tab and edit the form as given in the image below. Change the Name property of each text box to txtId, txtName, txtTitle, and txtSalary.
Edit the code as given in the images below.
Run the program and what you see is given in the image below.
How to Move Forward Through the Records of the Table
Add a button to the form as given in the image below and name it as btnNext.
Comments
Post a Comment