Auto–increment primary key in Microsoft SQL Server using vb.net

Here’s how you can add an auto-incrementing primary key in ms-sql. First, you need to add an ms-sql database as a datasource, in your vb.net application.

After adding the database.

Go to ‘view’ –> ‘server explorer’

Then select the datasource which you have added. In my case, it is ‘kyokasuigetsu.onstor.dbo’

Expand that, and you will see the ‘Tables’ folder. Select the table that you want to work with. In my case, it is the ‘prod_table’ . Then select ‘open table definition’ as you can see in the screenshot below.

image

Select the primary key of the table that you have chose. In my case it is the ‘PID’ field. 

Under  ‘column properties’. You will see the ‘Identity Specification. Expand that. And change the value of ‘Identity Specification’ to ‘yes’ by either double-clicking or just change its value from the properties pane. The (is identity) will also automatically change.

And the ‘identity  increment’ and ‘identity seed’ values will be automatically equal to one.

This means that you won’t have to insert a value to the primary key if you’re going to perform an insert query. The identity increment is the value which will be added to the primary key everytime you perform an insert query. The identity seed is the primary value of the primary key. This means that it will begin counting from 1 up to the last inserted value.

image

2 thoughts on “Auto–increment primary key in Microsoft SQL Server using vb.net

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s