ExecuteScalar vs ExecuteNonQuery when returning an identity value

前端 未结 1 1425
失恋的感觉
失恋的感觉 2020-11-30 05:15

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

相关标签:
1条回答
  • 2020-11-30 05:23

    As suggested by Aaron, a stored procedure would make it faster because it saves Sql Server the work of compiling your SQL batch. However, you could still go with either approach: ExecuteScalar or ExecuteNonQuery. IMHO, the performance difference between them is so small, that either method is just as "proper".

    Having said that, I don't see the point of using ExecuteScalar if you are grabbing the identity value from an output parameter. In that case, the value returned by ExecuteScalar becomes useless.

    An approach that I like because it requires less code, uses ExecuteScalar without output parameters:

    public static int SaveTest(Test newTest)
    {
        var conn = DbConnect.Connection();
        const string sqlString = "INSERT INTO dbo.Tests ( Tester , Premise ) " +
                                 "               VALUES ( @tester , @premise ) " +
                                 "SELECT SCOPE_IDENTITY()";
        using (conn)
        {
            using (var cmd = new SqlCommand(sqlString, conn))
            {
                cmd.Parameters.AddWithValue("@tester", newTest.tester);
                cmd.Parameters.AddWithValue("@premise", newTest.premise);
    
                cmd.CommandType = CommandType.Text;
                conn.Open();
                return (int) (decimal) cmd.ExecuteScalar();
    
            }
        }
    }
    

    Happy programming!

    EDIT: Note that we need to cast twice: from object to decimal, and then to int (thanks to techturtle for noting this).

    0 讨论(0)
提交回复
热议问题