Re: ID of a newly created record
From: 2 (2_at_2.2)
Date: 03/03/04
- Next message: Crad: "RE: Data Relations and Ill pay someone now"
- Previous message: Crad: "RE: ID of a newly created record"
- In reply to: Glyn Williams: "ID of a newly created record"
- Next in thread: Glyn Williams: "Re: ID of a newly created record"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 3 Mar 2004 19:47:44 +1100
However you choose to implement this you should be aware of the difference
between @@identity and scope_identity()...
@@identity is not limited in scope whereas scope_identity() will return the
id of the last inserted record in the current scope... in other words if
lots of inserts are happening on the database server you may get the wrong
identity returned using @@identity... so generally you'd use scope_identity
Look it up in SQL Books Online...
Cheers
"Glyn Williams" <orange_email@yahoo.co.uk> wrote in message
news:%23hJkdIJAEHA.2040@TK2MSFTNGP12.phx.gbl...
> Hi,
> I'm trying to get the ID of a newly created record into a variable, so I
can
> display it in a Label or something after being created. I know you can use
> @@identity in the sql statement, but I just can't seem to get it to go.
>
> Any help appreciated,
> Glyn Williams
>
> Here is my code:-
> Public Sub addBike(Sender As Object, E As EventArgs)
>
> Dim myCommand As new SqlCommand()
> Dim myConnection As New SqlConnection
> ("server=Tron;database=sellmybike;Integrated Security=SSPI")
>
> myCommand.Connection = myConnection
> 'Define insert command
> myCommand.CommandText="INSERT INTO Details
>
(PostDate,Name,Town,County,Seller,Email,Tel,Make,Model,ManYear,Mileage,Price
> ,Details) VALUES (
>
@PostDate,@Name,@Town,@County,@Seller,@Email,@Tel,@Make,@Model,@ManYear,@Mil
> eage,@Price,@Details)"
>
> myCommand.Parameters.Add ("@PostDate", Now())
> myCommand.Parameters.Add ("@Name", msgName.Value)
> myCommand.Parameters.Add ("@Town", msgTown.Value)
> myCommand.Parameters.Add ("@County", msgCounty.Value)
> myCommand.Parameters.Add ("@Seller", msgSeller.Value)
> myCommand.Parameters.Add ("@Email", msgEmail.Value)
> myCommand.Parameters.Add ("@Tel", msgTel.Value)
> myCommand.Parameters.Add ("@Make", msgMake.Value)
> myCommand.Parameters.Add ("@Model", msgModel.Value)
> myCommand.Parameters.Add ("@ManYear", msgManYear.Value)
> myCommand.Parameters.Add ("@Mileage", msgMileage.Value)
> myCommand.Parameters.Add ("@Price", msgPrice.Value)
> myCommand.Parameters.Add ("@Details", msgDetails.Value)
>
> myCommand.Connection.Open()
> 'update the tape
> myCommand.ExecuteNonQuery()
>
> 'Im sure some code must go here and in the sql statement
>
> myCommand.Connection.Close()
> End Sub
>
>
- Next message: Crad: "RE: Data Relations and Ill pay someone now"
- Previous message: Crad: "RE: ID of a newly created record"
- In reply to: Glyn Williams: "ID of a newly created record"
- Next in thread: Glyn Williams: "Re: ID of a newly created record"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|