Wednesday, April 18, 2012

How to add Add DataBase in Visual Studio

Hello everyone today I am gonna teach you how you can add database in your project in Visual Studio, I will also show the method to make a primary key (Which is unique), and Identity Specification which increment whenever there is a new entry.

1. Create a project in VS

2. Under solutin Explorer, right click on your database hover your mouse to "Add" and click on "New Item"

3. Select "SQL Database" from the list, and click on OK

4.  Now open up your Server Explorer or press Ctrl + W + l

5. You will find your database .mdf  in server Explorer , double click on it to expand

6. Under your database right click on "Tables", and select "Add New Table"

7. You will see three column Column Name, Data Type, Allow Nulls (By default)

8. I have defined 3 columns  like below


9. Now we will set the Student ID as the primary key, to do this right click on the student ID and select "Set Primary Key"

10. Now if we want the student ID to be auto incremented whenever a new record is being added, we will need to set its "Identity Specification"

11. Right click on Student ID column, and below you will find "Column Properties", in Column properties search for "Identity Specification", expand it and you will find "Is Identity". By default it is set to No, so double click on it and it will be set as Yes.

12. Now you can set the identity Increment & Identity Seed according to your need, by default both are set to 1

To View your tables, open up Server Explorer right click on your table and select Show Table Data

I hope you enjoyed the tutorial, if you are still having problem than please post a comment

Thanks.

No comments:

Post a Comment