示例#1
0
        internal static Vehicle GetVehicle(int driverId)
        {
            Vehicle tmp = new Vehicle();
            string strDBConnection = Helper.DBHelper.getDbConnectionString();
            string sqlQuery = "SELECT * FROM dbo.Vehicle WHERE ID = {0}";
            sqlQuery = String.Format(sqlQuery, driverId);
            SqlConnection dbCon = new SqlConnection(strDBConnection);
            SqlCommand dbCmd = new SqlCommand();

            try
            {
                if (dbCon.State == ConnectionState.Closed) { dbCon.Open(); }
                dbCmd.Connection = dbCon;
                dbCmd.CommandType = CommandType.Text;
                dbCmd.CommandText = sqlQuery;

                SqlDataReader dbRdr = dbCmd.ExecuteReader();
                int iTest = 0;
                while (dbRdr.Read())
                {
                    tmp = GetVehicle(dbRdr);
                    iTest++;
                }
                if (iTest > 1)
                {
                    throw new Exception("Expect to get one vehicle, but more drivers are got.");
                }
            }
            catch (SqlException se) { throw se; }
            catch (Exception e) { throw e; }
            finally { dbCon.Close(); }

            return tmp;
        }
示例#2
0
        /// <summary>
        /// Create a driver object from the SqlDataReader
        /// </summary>
        /// <param name="dbRdr">From the SqlDataReader</param>
        /// <returns>Returns the driver object from IDataReader</returns>
        /// <exception cref="InvalidCastException">InvalidCastException may be thrown</exception>
        internal static Vehicle GetVehicle(IDataReader dbRdr)
        {
            Vehicle tmp;
            try
            {
                tmp = new Vehicle(Convert.ToInt32(dbRdr["ID"]),
                               Convert.ToInt32(dbRdr["VehicleType"]),
                               Convert.ToDecimal(dbRdr["MaxLoad"]),
                               Convert.ToInt32(dbRdr["IsAvailable"]) == 0 ? false : true
                               );
            }
            catch (InvalidCastException ice) { throw ice; }

            return tmp;
        }