Visual Studio IDE comes with a free SQL database called SQL Server. When a user fill the form and submit it we have to save the data submitted in a database. Here we will discus, how to create database for saving user’s data. We will create a database and add a table in that database in this article.
Database can be created with the following simple steps.
- Right click the App_Data folder in the Solution Explorer
- Select Add, then select new item
- Select SQL Server Database
- Name the Database like BookStore.mdf
- Click the Add button
After database has been created, double click on the database or press Ctrl + Alt + S to open server explorer as shown in the figure below;
Now we will create a table where data submitted by the user will be stored. Go to sever explorer, expand the folder then right click on the Table folder. Click on Add New Table and then by T-SQL command of Create Table, we are creating table as shown in the code below;
CREATE TABLE [dbo].[Book] (
[AuthorName] VARCHAR (250) NOT NULL,
[Title] VARCHAR NULL,
[PublisherName] VARCHAR NULL,
[PublishDate] DATE NULL,
PRIMARY KEY CLUSTERED ([AuthorName] ASC)
We are creating four columns in the table Book. The first three columns AuthorName, Title, PublisherName are text columns having datatype Varchar and the fouth column PublishDate has datatype Date to store publish date. Here we declare the AuthorName as Primary Key.
After adding a table, click on Update link on the top left corner. A new window ‘Preview Database Updates’ will open as shown below;
Click on update Database and a message will appear that shows update completed successfully as shown above. So in this way, we are creating a new table in our database ‘BookStore’.