Auto-Index Generation in C # .Net by Encoding

hello sir. In a C # .net project, I have code to generate an automatic index, it works fine. I used logic as shown below; 1st counting the number of rows in the table, and then extracting what value was stored in the table. Then no matter what value is incremented by 1 for the next field, and therefore this is the next index. There is a problem when the index is greater than 10. The index in the table is saved as it is 1 10 11 2 3 4 5 6 7 8 9

therefore, the last value in the table is 9, and the next increment will be 10, which is already so that this will be an error violating the main key.

So, plz guide how to achieve automatic index generation in my project.

+3
source share
3 answers

Why don't you do column identifier column like this

CREATE TABLE [dbo].[Foo](
    [Foo] [int] IDENTITY(1,1) NOT NULL,
    //Other columns
)

or change if the database is already created

ALTER TABLE [dbo].[Foo](
    [Foo] [int] IDENTITY(<Enter your last maximum number in the database>,1) NOT NULL,
    //Other columns
)

Hope this helps

+2
source

Instead of COUNTING ROWS, you need to get the MAX value from the table for this field / column, here, in accordance with the given values, it is 11, and then increase it by +1.

So you need to use the following code in the code,

    Select Max(Key Field) + 1 from [table name]//this will give you new key value i.e. 12

I suggest you use the Identity column, so you don’t have to worry about raising the primary key values.

+2
source

Check the data type of your autoindex ... I have a doubt that it is treated as a string. The first thing you need to do is determine the data type for the whole. So far, your seeding is in order.

0
source

Source: https://habr.com/ru/post/1786831/


All Articles