executescalar

ExecuteScalar vs ExecuteNonQuery when returning an identity value

蹲街弑〆低调 提交于 2019-11-27 04:05:09
Trying to figure out if it's best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a few weeks ago (whilst heavily borrowing from this site) I found that in my inserts I was using ExecuteScalar , like so: public static int SaveTest(Test newTest) { var conn = DbConnect.Connection(); const string sqlString = "INSERT INTO dbo.Tests ( Tester , Premise ) " + " VALUES ( @tester , @premise ) " + "SET @newId = SCOPE_IDENTITY(); "; using (conn

ExecuteScalar vs ExecuteNonQuery when returning an identity value

。_饼干妹妹 提交于 2019-11-26 11:02:10
问题 Trying to figure out if it\'s best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a few weeks ago (whilst heavily borrowing from this site) I found that in my inserts I was using ExecuteScalar , like so: public static int SaveTest(Test newTest) { var conn = DbConnect.Connection(); const string sqlString = \"INSERT INTO dbo.Tests (