public DriverModel Get(int id)
        {
            DriverModel obj = new DriverModel();

            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    string tsql = "sp_getDriver";

                    using (SqlCommand cmd = new SqlCommand(tsql, conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@IDDriver", id);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                obj = new DriverModel
                                {
                                    IDDriver       = (int)reader["IDDriver"],
                                    Name           = reader["Name"].ToString(),
                                    Picture        = reader["Picture"].ToString(),
                                    Status         = reader["Status"].ToString(),
                                    ActualPosition = new PositionModel
                                    {
                                        Latitude  = reader["Latitude"].ToString(),
                                        Longitude = reader["Longitude"].ToString()
                                    }
                                };
                            }
                        }
                    }
                }
                return(obj);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        public DriverModel Get(int id)
        {
            DriverModel driver = new DriverModel();

            try
            {
                using (SqlConnection conn = new SqlConnection(ConnectionString))
                {
                    conn.Open();
                    string tsql = "SELECT * FROM Driver INNER JOIN Position ON Driver.IDActualPosition = Position.IDPosition WHERE IDDriver = @IDDriver";
                    using (SqlCommand cmd = new SqlCommand(tsql, conn))
                    {
                        cmd.Parameters.AddWithValue("@IDDriver", id);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                driver = new DriverModel
                                {
                                    IDDriver       = (int)reader["IDDriver"],
                                    Name           = reader["Name"].ToString(),
                                    Status         = reader["Status"].ToString(),
                                    Picture        = reader["Picture"].ToString(),
                                    ActualPosition = new PositionModel
                                    {
                                        Latitude  = reader["Latitude"].ToString(),
                                        Longitude = reader["Longitude"].ToString()
                                    }
                                };
                            }
                        }
                    }
                }
                return(driver);
            }
            catch (Exception)
            {
                throw;
            }
        }