public double Insert(User user, Stopwatch stopwatch) { double execTime; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand()) { command.Connection = sqlConnection; command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO [dbo].[User] (Id, FirstName, DateAdded, LastName, Age, DepartmentId) VALUES (@id, @firstN, @dateAdd, @lastN, @ag, @depId)"; command.Parameters.AddWithValue("@id", user.Id); command.Parameters.AddWithValue("@firstN", user.FirstName); command.Parameters.AddWithValue("@dateAdd", user.DateAdded); command.Parameters.AddWithValue("@lastN", user.LastName); command.Parameters.AddWithValue("@ag", user.Age); command.Parameters.AddWithValue("@depId", user.DepartmentId); stopwatch.Restart(); command.ExecuteNonQuery(); stopwatch.Stop(); execTime = ((double)stopwatch.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency) * 1000; } sqlConnection.Close(); } return execTime; }
public double SelectUserByKey(int key, Stopwatch stopwatch) { double execTime; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); using (SqlCommand command = new SqlCommand()) { command.Connection = sqlConnection; command.CommandType = CommandType.Text; command.CommandText = "SELECT Id, FirstName, LastName, Age, DepartmentId, DateAdded FROM [dbo].[User] WHERE Id = @id"; command.Parameters.AddWithValue("@id", key); stopwatch.Restart(); using (SqlDataReader rdr = command.ExecuteReader()) { if (rdr.HasRows) { while (rdr.Read()) { User user = new User { Id = rdr.GetInt32(0), FirstName = rdr.GetString(1), LastName = rdr.GetString(2), Age = rdr.GetInt32(3), DepartmentId = rdr.GetInt32(4), DateAdded = rdr.GetDateTime(5), }; } } stopwatch.Stop(); execTime = ((double)stopwatch.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency) * 1000; rdr.Close(); } } sqlConnection.Close(); } return execTime; }