Anyone know sql in c#?

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

I need to get the identity as soon as it is created at INSERT, but I'm
not haveing any luck at it. Here is my code:

string strSQL5 = "INSERT INTO tblTravelDetail(MemberId, " +
" Comments, " +
" TravelEventId, " +
" OrderId, " +
" ItemID, " +
" PeriodID, " + //here is where we do the
PeriodID lookup
calculation
" CreatedDateTime, " +
" Operator) " +
"VALUES ('" + deiinsertString2 + "', " +
" '" + deiinsertString3 + "', " +
" '" + eiinsertString1 + "', " +
" '" + deiinsertString5 + "', " +
" '" + deiinsertString6 + "', " +
" '" + deiinsertString7 + "', " +
" '" + deiinsertString8 + "', " +
" '" + insertString7 + "') " +
"SELECT @@IDENTITY AS 'Identity' ";

displayIdentity =
Convert.ToString(rs5.Fields["Identity"].Value); //<--this just gets
the identity for the first insert, not the very last one like I need.

Any help is appreciated.
Thanks,
Trint
 
Do u use a dataadapter or a datareader? Your answer depends on this.
I think u use datareader my advice u to use a dataadapter. Reloading your
data with data adapter after any change.

It is possible to do it with a datareader. But u should use stored
procedures instead of a concated statement. It is more secure....
 
Yunus,
I first want to know how to do this this way...then I will do a stored
proc.

DataSet ds5 = new DataSet("Recordset");
Can you look at the previous code and just say how to get the current
identity?
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
Ok. Forget dataset just use a stored procedure and SqlCommand. Use
ExecuteScalar method of SqlCommand and implement your sp something like
this;

INSERT INTO .... VALUES(...)
SELECT @@IDENTITY
 
Back
Top