Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Execute Scalar in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 47
    Comment on it

    In ADO.NET while writing codes we perform crud operations like Insert Update Delete.

    While performing these crud operations we always use SqlCommand class with the ExecuteNonQuery method for doing it.

    1. SqlCommand cmdInsert=new SqlCommand();
    2.  
    3. cmdInsert.Open();
    4.  
    5. cmdInsert.ExecuteNonQuery();

     

    We need to know that ExecuteScalar method can also be used for this purpose.

    This method returns a single value as the return type after the execution is done.

    If your  Result Set contains more than one columns or rows ,it will take value of first column and first row rest values will be ignored.

    We can also use this method while performing crud operations.

     

    1. List<SqlParameter> parameterList = new List<SqlParameter>();
    2.  
    3. parameterList.Add(new SqlParameter("@FirstName", user.FirstName));
    4.  
    5. parameterList.Add(new SqlParameter("@LastName", user.LastName));
    6.  
    7. parameterList.Add(new SqlParameter("@JobRoleID", user.JobRoleId));
    8.  
    9. parameterList.Add(new SqlParameter("@Email", user.Email));
    10.  
    11. parameterList.Add(new SqlParameter("@CompanyID", user.CompanyId));
    12.  
    13. parameterList.Add(new SqlParameter("@Password", user.Password));
    14.  
    15. parameterList.Add(new SqlParameter("@PasswordSalt", Guid.NewGuid().ToString()));
    16.  
    17. parameterList.Add(new SqlParameter("@AccessToken", accessToken));
    18.  
    19. parameterList.Add(new SqlParameter("@DeviceUUID", String.IsNullOrEmpty(user.DeviceId) ? String.Empty : user.DeviceId));
    20.  
    21. parameterList.Add(new SqlParameter("@DeviceType", String.IsNullOrEmpty(user.DeviceType) ? String.Empty : user.DeviceType));
    22.  
    23. parameterList.Add(new SqlParameter("@CreatedDate", System.DateTime.Now));
    24.  
    25. parameterList.Add(new SqlParameter("@IsDeleted", false));
    26.  
    27. parameterList.Add(new SqlParameter("@DeletedDate", System.DBNull.Value));
    28.  
    29.  
    30. if (sqlConnection.State == ConnectionState.Closed) {
    31.     sqlConnection.Open();
    32. }
    33.  
    34. result = Convert.ToInt32(BaseRepository.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "uspRegisterUser", parameterList.ToArray()));
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: