static void Main(string[] args)
        {
            string choice = string.Empty, dbProvider = string.Empty;
            bool   done = false;

            do
            {
                Console.Clear();
                Console.WriteLine("\t Select one of the Database Providers \n");
                Console.WriteLine("\t 1. Access / OleDb");
                Console.WriteLine("\t 2. Odbc");
                Console.WriteLine("\t 3. Oracle");
                Console.WriteLine("\t 4. Sql \n");
                Console.WriteLine("================================================================================");
                Console.Write("\t Enter Your Selection (0 to exit) : ");
                choice = Console.ReadLine();

                switch (choice)
                {
                case "0":
                    done = true;
                    break;

                case "1":
                    dbProvider = "Access";
                    break;

                case "2":
                    dbProvider = "Odbc";
                    break;

                case "3":
                    dbProvider = "Oracle";
                    break;

                case "4":
                    dbProvider = "Sql";
                    break;
                }
                if (choice != "0")
                {
                    Console.WriteLine("================================================================================");
                    DALFactory Factory = new DbDALFactory();
                    var        DAL     = Factory.GetDataAccessLayer((DataProviderType)Enum.Parse(typeof(DataProviderType), dbProvider));
                    Console.WriteLine(DAL.OpenConnection());
                    Console.ReadKey();
                }
            } while (!done);
        }
示例#2
0
        public override bool Update(Customer instance, out Exception exError)
        {
            exError = null;

            try
            {
                var connectionString = Properties.Resources.MsSQLConnectionString;
                var connectionQuery  = Properties.Resources.MsSQLUpdate;

                DALFactory Factory    = new DbDALFactory();
                var        DAL        = Factory.GetDataAccessLayer(DataProviderType.MsSQL);
                var        connection = DAL.OpenConnection(connectionString);

                while (connection.State != ConnectionState.Open)
                {
                    Console.WriteLine("Connecting...");
                    connection = DAL.OpenConnection(connectionString);
                }

                using (IDbCommand command = DAL.GeDataProviderCommand())
                {
                    command.CommandText = connectionQuery;
                    command.CommandType = CommandType.Text;
                    command.Connection  = connection;

                    command.Parameters.Add(AddParam(command, "@Id", instance.Id));
                    command.Parameters.Add(AddParam(command, "@FirstName", instance.FirstName));
                    command.Parameters.Add(AddParam(command, "@LastName", instance.LastName ?? ""));
                    command.Parameters.Add(AddParam(command, "@Email", instance.Email));
                    command.Parameters.Add(AddParam(command, "@Password", instance.Password));
                    command.Parameters.Add(AddParam(command, "@BirthDate", instance.BirthDate));
                    IDataReader reader = command.ExecuteReader();

                    command.Connection.Close();
                }
            }
            catch (InvalidOperationException invalid)
            {
                exError = invalid;
                return(false);
            }
            catch (Exception ex)
            {
                exError = ex;
                return(false);
            }

            return(true);
        }
示例#3
0
        public override bool Delete(int ID, out Exception exError)
        {
            exError = null;

            try
            {
                var connectionString = Properties.Resources.MsSQLConnectionString;
                var connectionQuery  = Properties.Resources.MsSQLDelete;

                DALFactory Factory    = new DbDALFactory();
                var        DAL        = Factory.GetDataAccessLayer(DataProviderType.MsSQL);
                var        connection = DAL.OpenConnection(connectionString);

                while (connection.State != ConnectionState.Open)
                {
                    Console.WriteLine("Connecting...");
                    connection = DAL.OpenConnection(connectionString);
                }

                using (IDbCommand command = DAL.GeDataProviderCommand())
                {
                    command.CommandText = connectionQuery;
                    command.CommandType = CommandType.Text;
                    command.Connection  = connection;

                    command.Parameters.Add(AddParam(command, "@Id", ID));
                    IDataReader reader = command.ExecuteReader();

                    command.Connection.Close();
                }
            }
            catch (InvalidOperationException invalid)
            {
                exError = invalid;
                return(false);
            }
            catch (Exception ex)
            {
                exError = ex;
                return(false);
            }

            return(true);
        }
示例#4
0
        public override List <Customer> Select(DataProviderType providerType, out Exception exError)
        {
            List <Customer> returnValue = new List <Customer>();

            exError = null;

            try
            {
                var connectionString = string.Empty;
                var connectionQuery  = string.Empty;
                switch (providerType)
                {
                case DataProviderType.MsSQL:
                    connectionString = Properties.Resources.MsSQLConnectionString;
                    connectionQuery  = Properties.Resources.MsSQLSelect;
                    break;

                case DataProviderType.MySQL:
                    connectionString = Properties.Resources.MySQLConnectionString;
                    connectionQuery  = Properties.Resources.MySQLSelect;
                    break;

                default:
                    break;
                }
                DALFactory Factory    = new DbDALFactory();
                var        DAL        = Factory.GetDataAccessLayer(providerType);
                var        connection = DAL.OpenConnection(connectionString);

                while (connection.State != ConnectionState.Open)
                {
                    Console.WriteLine("Connecting...");
                    connection = DAL.OpenConnection(connectionString);
                }

                using (IDbCommand command = DAL.GeDataProviderCommand())
                {
                    command.CommandText = connectionQuery;
                    command.CommandType = CommandType.Text;
                    command.Connection  = connection;

                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            returnValue.Add(new Customer()
                            {
                                Id        = reader.GetInt32(0),
                                FirstName = !String.IsNullOrEmpty(reader.GetString(1)) ? reader.GetString(1) : null,
                                LastName  = !String.IsNullOrEmpty(reader.GetString(2)) ? reader.GetString(2) : null,
                                Email     = reader.GetString(3),
                                Password  = reader.GetString(4),
                                BirthDate = reader.GetDateTime(5) != null ? reader.GetDateTime(5) : DateTime.MinValue,
                                isDelete  = false
                            });
                        }
                    }
                    command.Connection.Close();
                }
            }
            catch (InvalidOperationException invalid)
            {
                exError = invalid;
            }
            catch (Exception ex)
            {
                exError = ex;
            }

            return(returnValue);
        }