Can I use ExecuteScalar for updates?

I am using SQL Server 2005 and C #. I want to get the primary key value when I insert records into the database.

For this, I have a C # method in which I insert and update records. I use the method ExecuteScalarto insert and update.

The insert was successful, but there are no updates. How can I use ExecuteScalarto get the primary key for updates?

+3
source share
2 answers

, ExecuteScalar , (, SELECT). , - , ExecuteNonQuery.

ExecuteNonQuery . , :

INSERT INTO TableName(someFields) VALUES (someValues)
SET @Id = SCOPE_IDENTITY()

SET @Id = newid()
INSERT INTO (ID, someFields) VALUES (@id, someValues)

uniqueidentifier .

output @Id , ExecuteNonQuery .

+3

ExecuteScalar ( /), ExecuteNonQuery , , . , ? - , ? - . (.. ), , ( ). , .

0

Source: https://habr.com/ru/post/1764474/


All Articles