Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • ExecuteScalar while CRUD operations

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 41
    Comment on it

    While performing the CRUD operations like insert delete update in the database we make use of the functions provided by the SQL classes.

     

     

     

    Mostly we use ExecuteNonQuery function for the CRUD operations but we need to understand which one to use in which time.

     

    If your operation is going to update/alter  only one record at a time then you can use ExecuteScalar instead of  ExecuteNonQuery .

     

    ExecuteScalar also does the same operation and you can also read data from it using the data reader.

     

    ExecuteNonQuery you need to get the data read by specifying it as the OUT parameters.

     

    So for doing these things which effect only one record its better to use ExecuteScalar when you have returning records.

     

    
            public EmployeeModel UpdateUser(EmployeeModel EmpMod)
            {
                try
                {
                    int result = -1;
                    using (SqlConnection sqlConnection = new SqlConnection(strConnString))
                    {
                        List<SqlParameter> parameterList = new List<SqlParameter>();
                        parameterList.Add(new SqlParameter("@empId", EmpMod.EmpId));
                        parameterList.Add(new SqlParameter("@empFirstName", EmpMod.FirstName));
                        parameterList.Add(new SqlParameter("@empMiddleName", EmpMod.MiddleName));
                        parameterList.Add(new SqlParameter("@empLastName", EmpMod.LastName));
                        parameterList.Add(new SqlParameter("@empPhoneNo", EmpMod.PhoneNo));
                        parameterList.Add(new SqlParameter("@empAddress", EmpMod.Address));
                        if (String.Equals(sqlConnection.State, ConnectionState.Closed))
                            sqlConnection.Open();
                        result = Convert.ToInt32(SqlHelper.ExecuteScalar(strConnString, CommandType.StoredProcedure, "uspUpdateUser", parameterList.ToArray()));
                        EmpMod.Status = (result > 0 ? getstatus.success : getstatus.failure);
                    }
                }
                catch (SqlException ex)
                {
                    EmpMod.msg = ex.Message;
                }
                return EmpMod;
            }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: