Reputation:
I need to add a column name Serial Number to my one of the database table,so that it should increment for each data,How to write this code in asp.net with c#
My connection string is as shown below
cn.ConnectionString = @"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True";
My database table is shown as below
FID varchar(10) Checked
Placework varchar(50) Checked
DOJ datetime Checked
Institute varchar(50) Checked
Year numeric(18, 0) Checked
Duration varchar(50) Checked
Lastpost varchar(50) Checked
WorkingArea varchar(50) Checked
UpdateDate datetime Checked
In this table i need to add another column Sr.no that should automatically increment for every input and also FID and Sr.no should be composite primary key
Upvotes: 2
Views: 1315
Reputation: 39817
While this can be done in C# code, since it's a one time operation it's better to be done directly on DB.
This can even be done visually in the designer, just add new column to the table and mark it as Identity.
Alternatively SQL code for this is:
ALTER TABLE YourTableName
ADD SerialNumber int NOT NULL IDENTITY (1, 1)
This will create a column that will auto-increment its value starting with 1 with each record added.
Again, if needed this command can be issued from C# code via SqlCommand, but I would not recommend it.
Upvotes: 1
Reputation: 1021
In SQL, select the column that you want to increment. In the below pane in SQL Make Identity Specification to yes. Tour value of column will increase automatically.
Upvotes: 0