Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in inline insert query I can write
"insert into table id=newid()..."

In parameterized query in c#

"Insert into table (name,id) values (@name,@id)..."
sqlcommand.parameter.addwithvalue("@name","souvik");
sqlcommand.parameter.addwithvalue("@id",?);

how to use newid()?
Posted
Comments
Sanket Saxena 9-Jan-15 8:24am    
Why you are inserting ID via parameter. Any specific format you want? If not then just manage it via SQL no need to pass anything from here SQL will take care of it.

NEWID is a function, you can't use it as a parameter (you should call it explicitely in the insert statement). Please see the documentation: "NEWID (Transact-SQL)"[^].
 
Share this answer
 
Comments
souvikcode 9-Jan-15 6:53am    
Then how can I insert unique value in that id field?
souvikcode 9-Jan-15 7:05am    
I've tried this and this works.

"Insert into table (name,id) values (@name,newid())..."
sqlcommand.parameter.addwithvalue("@name","souvik");
I would suggest to use this function in default constraint for ID Columns

for Eg:

create table abc
(

name varchar(20) not null,
ID varchar (16) default(newID())

)

now write your insert statement as below:
Insert into ABC(name) values ('person1')


Note that you can use various datatype instead of varchar(16) as per your requirement.
 
Share this answer
 
insert into table (name,id) values (@name,newid())..."
sqlcommand.parameter.addwithvalue("@name","souvik");
 
Share this answer
 

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