While performing database operations like Insert,Update,Delete,Select we use Sql classes for doing it.
But instead of writing the entire code for classes we can just do it with the helper class DLL known as Sql Helper.
We need to first add the reference of that DLL into our project.
After that we can use the methods defined in that DLL in our code.
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("@FirstName", empobj.FirstName));
parameterList.Add(new SqlParameter("@LastName", empobj.LastName));
parameterList.Add(new SqlParameter("@JobRoleID", empobj.JobRoleId));
parameterList.Add(new SqlParameter("@Email", empobj.Email));
parameterList.Add(new SqlParameter("@CompanyID", empobj.CompanyId));
parameterList.Add(new SqlParameter("@Password", empobj.Password));
parameterList.Add(new SqlParameter("@PasswordSalt", Guid.NewGuid().ToString()));
parameterList.Add(new SqlParameter("@AccessToken", accessToken));
parameterList.Add(new SqlParameter("@DeviceUUID", empobj.DeviceId));
parameterList.Add(new SqlParameter("@DeviceType", empobj.DeviceType));
parameterList.Add(new SqlParameter("@CreatedDate", System.DateTime.Now));
parameterList.Add(new SqlParameter("@IsDeleted", empobj.IsDeleted));
parameterList.Add(new SqlParameter("@DeletedDate", System.DateTime.Now));
result = Convert.ToInt32(SqlHelper.ExecuteNonQuery(sqlConnection, CommandType.StoredProcedure, "uspRegisterUser", parameterList.ToArray()));
If you look in this code we have made the use of SqlHelper class without creating any command object for insertion.
All the classes are already defined the SqlHelper class, what we need to do is to just call the method of that class and pass parameters into it.
SqlDataReader reader = null;
using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
{
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("@Email", emp.Email));
parameterList.Add(new SqlParameter("@Password", emp.Password));
parameterList.Add(new SqlParameter("@DeviceUUID", emp.DeviceId));
parameterList.Add(new SqlParameter("@DeviceType", emp.DeviceType));
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspAuthenticateUser", parameterList.ToArray());
if (reader.HasRows)
{
while (reader.Read())
{
empResponse.accessToken = reader["AccessToken"] != null ? Convert.ToString(reader["AccessToken"]) : String.Empty;
fullName = reader["FullName"] != null ? Convert.ToString(reader["FullName"]) : String.Empty;
empResponse.Status = true;
empResponse.Message = "User successfully logged in.";
break;
}
}
else
{
empResponse.Status = false;
empResponse.Message = "Login failed.";
}
}
0 Comment(s)