Пример #1
0
        static void Main(string[] args)
        {
            Devart.Data.SQLite.SQLiteConnection connection = new Devart.Data.SQLite.SQLiteConnection(@"Data Source=..\..\Database\database.db");
            connection.Open();

            Devart.Data.SQLite.SQLiteScript script = new Devart.Data.SQLite.SQLiteScript(@"
DELETE FROM Department;

INSERT INTO Department VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO Department VALUES (20,'RESEARCH','DALLAS');
INSERT INTO Department VALUES (30,'SALES','CHICAGO');
INSERT INTO Department VALUES (40,'OPERATIONS','BOSTON');
", connection);
            script.Execute();

            Console.WriteLine("");
            Console.WriteLine("Departments count: 4.");
            Console.WriteLine("");
            Console.WriteLine("Execute InsertDepartment query. SQL:");

            ISessionFactory sessionFactory        = new Configuration().AddAssembly(typeof(Program).Assembly).BuildSessionFactory();
            ISession        session               = sessionFactory.OpenSession();
            databaseModelMethodsExecutor executor = new databaseModelMethodsExecutor(session);

            executor.InsertDepartment(50, "MANAGERS", "NEW YORK");

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("Retrieve Departments. SQL:");

            IList <Department> result = session.CreateQuery("from Department").List <Department>();

            Console.WriteLine("");
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Departments:");
            Console.WriteLine("------------------------------------------------------");
            foreach (Department Department in result)
            {
                Console.WriteLine(string.Format("{0}: '{1}' '{2}'", Department.Id, Department.Name, Department.Location));
            }

            Console.Read();
        }
Пример #2
0
        static void Main(string[] args)
        {
            bool            isFluentMapping = false;
            ISessionFactory sessionFactory;

            if (isFluentMapping)
            {
                sessionFactory = Fluently.Configure().Mappings(m => m.FluentMappings.AddFromAssemblyOf <Program>()).BuildSessionFactory();
            }
            else
            {
                sessionFactory = new Configuration().AddAssembly(typeof(Program).Assembly).BuildSessionFactory();
            }

            Devart.Data.SQLite.SQLiteConnection connection = new Devart.Data.SQLite.SQLiteConnection(@"Data Source=..\..\Database\database.db");
            connection.Open();

            Devart.Data.SQLite.SQLiteScript script = new Devart.Data.SQLite.SQLiteScript(@"
DELETE FROM Department;

INSERT INTO Department(DEPTNO, DNAME, LOC) VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO Department(DEPTNO, DNAME, LOC) VALUES (20,'RESEARCH','DALLAS');
INSERT INTO Department(DEPTNO, DNAME, LOC) VALUES (30,'SALES','CHICAGO');
INSERT INTO Department(DEPTNO, DNAME, LOC) VALUES (40,'OPERATIONS','BOSTON');
", connection);
            script.Execute();


            ISession           session = sessionFactory.OpenSession();
            IList <Department> result  = session.CreateQuery("from Department").List <Department>();

            Devart.Data.SQLite.SQLiteCommand command = new Devart.Data.SQLite.SQLiteCommand("UPDATE Department SET LOC = 'SAN FRANCISCO' WHERE DEPTNO = 10", connection);
            command.ExecuteNonQuery();

            // when you try to update an object Hibernate will check whether the value of the version contained in the object matches that held in the database.
            // If they don't match Hibernate throws a StaleObjectException. If they do Hibernate updates the object incrementing the version.
            result[0].LOC = "WASHINGTON";
            session.Flush();
        }