Code:
namespace CarsULike
{
class AddCarDataManager
{
static SqlCommand GetSelectCommand()
{
SqlConnection connection = CarsULikeDB.GetConnection();
String sqlText = "SELECT * FROM Car";
SqlCommand command = new SqlCommand(sqlText, connection);
return command;
}
static SqlCommand GetInsertCommand()
{
SqlConnection connection = CarsULikeDB.GetConnection();
String sqlText = "INSERT INTO Car (CarModelID, RegNo, RegYearDate Description, Mileage, Colour, TransmissionType, FuelType, NumberDoors, ListPrice, Status) VALUES (@CarID, @CarReg, @CarYear, @CarDescription, @CarMileage, @CarColour, @CarType, @CarFuel, @CarEngine, @CarDoors, @CarPrice, @CarStatus)";
SqlCommand command = new SqlCommand(sqlText, connection);
command.Parameters.Add("@CarID", System.Data.SqlDbType.VarChar, 3, "CarModelID");
command.Parameters.Add("@CarReg", System.Data.SqlDbType.VarChar, 8, "RegNo");
command.Parameters.Add("@CarYear", System.Data.SqlDbType.VarChar, 30, "RegYearDate");
command.Parameters.Add("@CarDescription", System.Data.SqlDbType.VarChar, 100, "Description");
command.Parameters.Add("@CarMileage", System.Data.SqlDbType.VarChar, 6, "Mileage");
command.Parameters.Add("@CarColour", System.Data.SqlDbType.VarChar, 10, "Colour");
command.Parameters.Add("@CarType", System.Data.SqlDbType.VarChar, 10, "TransmissionType");
command.Parameters.Add("@CarFuel", System.Data.SqlDbType.VarChar, 8, "FuelType");
command.Parameters.Add("@CarEngine", System.Data.SqlDbType.VarChar, 4, "EngineSize");
command.Parameters.Add("@CarDoors", System.Data.SqlDbType.VarChar, 1, "NumberDoors");
command.Parameters.Add("@CarPrice", System.Data.SqlDbType.VarChar, 8, "ListPrice");
command.Parameters.Add("@CarStatus", System.Data.SqlDbType.VarChar, 10, "Status");
return command;
}
public static DataTable Cars()
{
SqlCommand command = GetSelectCommand();
DataTable carstable = null;
using (command.Connection)
{
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
carstable = new DataTable();
carstable.Load(reader);
}
}
return carstable;
}
public static Boolean AddNewCar(double CarID, String CarReg, String CarYear, String CarDescription, double CarMileage, String CarColour, String CarType, String CarFuel, String CarEngine, int CarDoors, double CarPrice, String CarStatus)
{
try
{
SqlCommand command = GetInsertCommand();
command.Parameters["@CarID"].Value = CarID;
command.Parameters["@CarReg"].Value = CarReg;
command.Parameters["@CarYear"].Value = CarYear;
command.Parameters["@CarDescription"].Value = CarDescription;
command.Parameters["@CarMileage"].Value = CarMileage;
command.Parameters["@CarColour"].Value = CarColour;
command.Parameters["@CarType"].Value = CarType;
command.Parameters["@CarFuel"].Value = CarFuel;
command.Parameters["@CarEngine"].Value = CarEngine;
command.Parameters["@CarDoors"].Value = CarDoors;
command.Parameters["@CarPrice"].Value = CarPrice;
command.Parameters["@CarStatus"].Value = CarStatus;