database
-
Hi I need somebody help. In my c# project i need primary key of the row that i insert into my table; what should i do? If it is necessary to write storedprocedure , how can i use it in my project?
Use a statment like this: string szStatement = "SET NOCOUNT ON; INSERT INTO tablename (column_1, column_2, ..., column_n) VALUES(?,?, ..., ?); SELECT @@IDENTITY AS ID;"; someDatabaseCommand = new SomeDatabaseCommand(dbConnection, szStatement); int nNewId = (int)someDatabaseCommand.ExecuteScalar();
Greetings Covean
-
Hi I need somebody help. In my c# project i need primary key of the row that i insert into my table; what should i do? If it is necessary to write storedprocedure , how can i use it in my project?
I use GUIDs as primary keys. When I need the key of a row I'm about to insert I create the GUID in the application with System.Guid.NewGuid() and then pass it in with the rest of the data. I never use autoincrement fields; they're so last century.
-
I use GUIDs as primary keys. When I need the key of a row I'm about to insert I create the GUID in the application with System.Guid.NewGuid() and then pass it in with the rest of the data. I never use autoincrement fields; they're so last century.
-
Thanks alot. Can you explain me that what is GUIDs? How can i use it? should i create it c# code?
farokhian wrote:
Can you explain me that what is GUIDs?
farokhian wrote:
should i create it c# code?
All you will need is to call Managed Call to create GUID
farokhian wrote:
How can i use it?
Guid.NewGUID().ToString()
Al thou I Don't recommend this approach for database. The process is very slow, and there you can get duplicates GUIDs. I Recommend using Integer ID, With Auto_Increment options. When inserting Row, in SQL command do not specify column ID and value. It will grammatically give free ID number -
Thanks alot. Can you explain me that what is GUIDs? How can i use it? should i create it c# code?
GUIDs a string values used for uniqueness (example: 33b61dbd-2640-4153-8556-9cb9ad3e4a71). They are great for ensuring a unique identifier but not so great for indexing as they are string and thus cause the DB to have to do a table scan in order sort the records properly. Indentity or auto-increment field types may be old-school but they have the advantage of not needing to scan the table for ordering and thus provide much better performance for indexing. This is important because the Primary Key is an Index, the primary index in fact. The main advantage a GUID provides is with database farms. A record with a GUID will always have the same GUID but the Indentity could get reset across servers if precausons are not taken. All that being said, I feel your original question was not answered. In order to answer your question though I would need a little bit more info as the answer could differ. How are you connecting to the DB? ADO.Net? Linq? What kind of database are you connecting to? Aceess? SQL? MySQL? Thank you, Eric Ritzie