Using a Database

Most of the websites now use a Database to save their data from where it can be accessed whenever needed. Lets take the example of Facebook, Google, Tumblr. and all other websites. They use a Database to use all the user related Text information.

The general example is the messages that you send to the friends on Facebook, posts that you share with your circles or the blogging posts that you share with followers on Tumblr. website.

Everytime you share the post there is a query execution on the server-side code which saves all this data to a database which is connected to the website.

I will assume you created the website using WebMatrix, StarterSite template. Lets continue the process.

Firstly go to the Database workspace, that is just at the left bottom of the WebMatrix. You’ll get to work with the Database without using any other software or management software for the database. That is why I love WebMatrix – All in one software.

You remember how we interacted with the form? Lets continue that job and save the text information that is shared with the website and save it to the Database table.

First, create a table inside the StarterSite database and name it as Forms. Go to database workspace –> Click on Create Table in the top banner and name it as Forms

Then write the First Column name as Id and select int for the database and check the boxes that are infront of it. To be clear and simple for you, the checkboxes that you are going to check mean that this column (Id) will be the primary column and its value will not match with any other of its own value, that’s why a userid of Facebook user is never similar to any other user.

Now name a new column as Text and set its datatype to nvarchar [max(4000)], nvarchar means n number or characters, where n will be the value in the (…). In this case the 4000 will be n.

A tricky thing here is what does 4000 actually mean?

This means that this column will contain a total number of 4000 characters, but the max term with it means a data of 2GB, since each character uses 2 bytes then this means that this column will accommodate upto 1 billion characters. That’s more like it isn’t it? But when you write a value without max, lets say 400 then only 400 characters will be written and others will truncate into a string and will be removed. First 400 will be saved and others will be discarded.

There are some other data types for Sql Server too. Some of them are, bit (which means true false) will only have 2 values, you need to write either True or False to it; it is a bool.

Image datatype is for binary data. You can save images to the database too, but that’s not a good idea for a developer. You can read all other data types here, they will guide you at best.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s