Пример #1
0
        static void Main(string[] args)
        {
            // *******************************************************************
             // Teste para o provedor OleDb através da biblioteca FreeSQLOleDb
             // *******************************************************************

             // define o comando de conexão
             string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=admin;Password=;Jet OLEDB:Database Password=;",
            Path.Combine(Directory.GetCurrentDirectory(), "teste.mdb"));

             // instancia o acesso ao banco de dados através da biblioteca FreeSQL
             var db = new FreeSQLOleDb(connString, null);

             try
             {
            // abre a conexão
            db.OpenConnection();
            Console.WriteLine("Conexão aberta.");

            // cria um novo cliente
            var cli = new Cliente();
            cli.Nome = "Zezinho";
            cli.DataNascimento = new DateTime(2016, 1, 1);

            // Observação: como o código é auto-numerado não é necessário informar o número;
            // quanto a propriedade ativo, é recomendado setar para true no construtor da classe,
            // do contrário será necessário informar a cada instância do objeto, uma vez que o
            // registro será gravado e marcado como excluído no banco de dados
            cli.Ativo = true;

            // insere o registro e recupera o novo código
            var novoCod = (int)db.Insert(cli)[0];
            Console.WriteLine("Cliente cadastrado com sucesso.");

            // faz a leitura do novo registro inserido
            var lerCli = db.Select<Cliente>(novoCod);
            Console.WriteLine("Consultando os dados do cliente cadastrado.");
            Console.WriteLine("   Código: " + lerCli.Codigo);
            Console.WriteLine("   Nome  : " + lerCli.Nome);
            Console.WriteLine("   Data nasc: " + lerCli.DataNascimento.ToString());
            Console.WriteLine("   Ativo: " + (lerCli.Ativo ? "Sim" : "Não"));
            Console.WriteLine("");

            Console.WriteLine("Digite um novo nome para o cliente: ");
            string novoNome = Console.ReadLine();

            // altera o nome do cliente
            lerCli.Nome = novoNome;

            // salva o cliente
            db.Update(lerCli);
            Console.WriteLine("Cliente alterado com sucesso.");

            // pesquisa de clientes
            // a função requer o nome do campo, o operador e o valor de pesquisa
            // quando o operador é suprimido a pesquisa por padrão utiliza "="
            var pesqNome = db.SelectSpecial<Cliente>("nome", "LIKE", "ze%");
            Console.WriteLine(string.Format("Consulta de clientes que no nome contenham 'ze' retornou {0} resultado(s).", pesqNome.Length));

            // para uso do operador IN é necessário atribuir uma matriz (array) de valores,
            // esse valor pode ser de qualquer tipo de dados
            var pesqCod = db.SelectSpecial<Cliente>("codigo", "IN", new int[] { 1, 2, 3 });
            Console.WriteLine(string.Format("Consulta de clientes com os códigos 1, 2 e 3 retornou {0} resultado(s).", pesqCod.Length));

            // use o método SelectAll para retornar todos os registros ativos da tabela
            var todos = db.SelectAll<Cliente>("nome");
            Console.WriteLine(string.Format("Consulta de todos os clientes retornou {0} resultado(s).", todos.Length));

            // excluindo o cliente
            db.Delete(lerCli);
            Console.WriteLine(string.Format("Cliente '{0}' excluído com sucesso.", lerCli.Nome));

            // limpa os recursos
            cli = null;
            lerCli = null;

            Environment.Exit(0);
             }
             catch (Exception ex)
             {
            Console.WriteLine("Ocorreu o seguinte erro ao abrir a conexão:\n" + ex.Message);
            db.CloseConnection();
            db = null;
            Environment.Exit(1);
             }
        }
Пример #2
0
        static void Main(string[] args)
        {
            // *******************************************************************
            // Testing for the OleDb provider through the FreeSQLOleDb library
            // *******************************************************************

            // sets the connection command
            string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=admin;Password=;Jet OLEDB:Database Password=;",
                                              Path.Combine(Directory.GetCurrentDirectory(), "teste.mdb"));

            // Instance access to the database through the FreeSQL library
            var db = new FreeSQLOleDb(connString, null);

            try
            {
                // open connection
                db.OpenConnection();
                Console.WriteLine("The connection is open.");

                // creates a new customer
                var c = new Customer();
                c.Name      = "Zezinho";
                c.BirthDate = new DateTime(2016, 1, 1);

                // Note: because the ID is auto-increment, it's not necessary to enter the number;
                // as the 'Active' property, it's recommended to set to true in the class constructor,
                // otherwise it will be necessary to inform each instance of the object, since the
                // record will be written and marked as deleted in the database.
                c.Active = true;

                // inserts the record and retrieves the new code
                var newID = (int)db.Insert(c)[0];
                Console.WriteLine("Customer registered successfully.");

                // reads the new inserted record
                var readCustomer = db.Select <Customer>(newID);
                Console.WriteLine("Consulting customer data registered.");
                Console.WriteLine("   ID    : " + readCustomer.ID);
                Console.WriteLine("   Name  : " + readCustomer.Name);
                Console.WriteLine("   Birth date: " + readCustomer.BirthDate.ToString());
                Console.WriteLine("   Active:" + (readCustomer.Active ? "Sim" : "Não"));
                Console.WriteLine("");

                Console.WriteLine("Enter a new name for the customer: ");
                string newNome = Console.ReadLine();

                // change the customer name
                readCustomer.Name = newNome;

                // updates customer record
                db.Update(readCustomer);
                Console.WriteLine("Customer successfully changed.");

                // customer search
                // the function requires the name of the field, the operator and the search value
                // when the operator is suppressed the search by default uses "="
                var findName = db.SelectSpecial <Customer>("nome", "LIKE", "ze%");
                Console.WriteLine(string.Format("Customer search that the name contain 'ze' {0} returned result (s).", findName.Length));

                // for use of the IN operator it is necessary to assign an array of values,
                // this value can be of any data type
                var findID = db.SelectSpecial <Customer>("codigo", "IN", new int[] { 1, 2, 3 });
                Console.WriteLine(string.Format("Customer search with id's 1, 2 and 3 returned {0} result (s).", findID.Length));

                // use the SelectAll method to return all active records in the table
                var all = db.SelectAll <Customer>("nome");
                Console.WriteLine(string.Format("Query from all clients returned {0} result (s).", all.Length));

                // excluding customer
                db.Delete(readCustomer);
                Console.WriteLine(string.Format("Customer '{0}' deleted successfully.", readCustomer.Name));

                // clean resources
                c            = null;
                readCustomer = null;

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The following error occurred while opening the connection:\n" + ex.Message);
                db.CloseConnection();
                db = null;
                Environment.Exit(1);
            }
        }