Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#5
hi
i'm bigginer at c#.please somebody help me!
i want to put an id as a primary key in my database and fill it automatically but in c# there is an error with my code,how i can do that?
plz help me!
Posted 22-Sep-11 19:21pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
In DataBase
 
On creation of table you specify Id as PrimaryKey and set Identity(1,1) then it takes care of filling in sequence
 
http://msdn.microsoft.com/en-us/library/aa933196(v=SQL.80).aspx[^]
 
On saving of entity you dont need to pass Id from application..
 
In c#
 
I dont think you need to do any thing...If you have a requirement in application to identify which property refers to PrimaryKey in datatable..then you can mark property is a Primary by means of Attribute..
 
more to know from
 
http://msdn.microsoft.com/en-us/library/z0w1kczw.aspx[^]
  Permalink  
Comments
Tech Code Freak at 23-Sep-11 0:45am
   
Good links!
My 5up!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can make the primary key as Identity column. Itz possible via create table statement or GUI table designer.
 
CREATE TABLE myTableName ( myPrimary INT IDENTITY(1, 1), ... )
 
It automatically inserts the value for myPrimary key starting from 1 and increment of 1. You can change the start and increment points in IDENTITY(1, 1) parameters
  Permalink  
v2
Comments
Tech Code Freak at 23-Sep-11 1:02am
   
Good Solution!
My 5up!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

DataTableName.PrimaryKey = new DataColumn() {DataTableName.Columns("ID")} u can set this way in C# Data table
 
as far database concern u add the the primay key in your Sql Table with this way
 
ALTER TABLE Customer ADD PRIMARY KEY (CustomerId);
 
if you are creating new SQl Table
 
CREATE TABLE MyTable (cola INT IDENTITY(1, 1) PRIMARY KEY ,Col2 varchar)
 
IDENTITY always generates new Id at the time of insertion of any record
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 23 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100