Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to save some data to one table from many PC. But the data have a index(ID) that fill manually and order from previous data(ID), so I check it first on database then increment it.The problem is If more than one PC/device save together, it will save with same result. How to solve this problem?

What I have tried:

This is query for search the last ID that used:
public DataRow GetPlayerIDByClient(object clientPK)
       {
           DataRow result = null;

           if (clientPK != null)
           {
               IDbDataParameter clientFKParam = this.CreateParameter(csPlayerNamesEntity.Names.ClientFK, clientPK);

               string selectCommand = string.Format("SELECT TOP 1 * FROM {0} WHERE {1} = {3} AND " +
                                                       "({2} LIKE 'G%' or {2} LIKE 'g%') ORDER BY {4} DESC",
                                                       csPlayerNamesEntity.Names.TableName, //0
                                                       csPlayerNamesEntity.Names.ClientFK, //1
                                                       csPlayerNamesEntity.Names.ID,//2
                                                       clientFKParam,//3
                                                       csPlayerNamesEntity.Names.PK); //4

               DataTable dt = this.ExecReaderDataTable(selectCommand, clientFKParam);

               if (dt != null && dt.Rows.Count > 0)
               {
                   result = dt.Rows[0];
               }
           }

           return result;
       }
Posted
Updated 18-Jul-17 20:23pm

1 solution

If you define your ID column as follows, it will be incremented automatically:
SQL
ID int IDENTITY(1,1) PRIMARY KEY
You can also set an initial value, see example here: Identity Columns - Simple Talk[^]

If that is not what you want, you could consider writing a trigger on insert that does what you want.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900