Example #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var connection = new H2Connection("jdbc:h2:mem:test");

            connection.Open();
            new H2Command("create table list (item integer primary key, description varchar(256), value integer)", connection).ExecuteNonQuery();
            new H2Command("insert into list values (1, 'First Item', 10)", connection).ExecuteNonQuery();
            new H2Command("insert into list values (2, 'Second Item', 11)", connection).ExecuteNonQuery();

            var table = new DataTable("test")
            {
                CaseSensitive = false
            };
            var adapter = new H2DataAdapter("select item, description, value from list order by item", connection);

            new H2CommandBuilder(adapter);
            adapter.Fill(table);

            table.Rows[1][1] = "First item modified";
            table.Rows[1][2] = 12;
            table.Rows.Add(new object[] { 3, "Third", 15 });
            adapter.Update(table);

            var table2 = new DataTable("test")
            {
                CaseSensitive = false
            };

            adapter.Fill(table2);

            var x1 = table.ToXml();
            var x2 = table2.ToXml();

            Debug.Assert(x1.Equals(x2));

            var count = new H2Command("select count(*) from list", connection).ExecuteScalar();

            Debug.Assert(((long)count).Equals(3));

            var one = new H2Command("select 1 from dual", connection).ExecuteScalar();

            Debug.Assert(((int)one).Equals(1));

            var a = new H2Command("select 'a' from dual", connection).ExecuteScalar();

            Debug.Assert(((string)a).Equals("a"));

            SimpleTest(connection, "int", 10, 11, 12);
            SimpleTest(connection, "bigint", (long)10, (long)11, (long)12);
            SimpleTest(connection, "smallint", (short)10, (short)11, (short)12);
            SimpleTest(connection, "tinyint", (byte)10, (byte)11, (byte)12);
            SimpleTest(connection, "double", 10d, 11d, 12d);
            SimpleTest(connection, "float", 10d, 11d, 12d); // double is float !
            SimpleTest(connection, "varchar(255)", "10", "11", "12");
            SimpleTest(connection, "timestamp", DateTime.Today, DateTime.Today.AddDays(1), DateTime.Today.AddDays(2));
            SimpleTest(connection, "date", DateTime.Today, DateTime.Today.AddDays(1), DateTime.Today.AddDays(2));
            //var x1 = table.ToXml();
        }
Example #2
0
        static void Main(string[] args)
        {
            using (var connection = new H2Connection("connectionString", "username", "password"))
            {
                connection.Open();

                H2Command command = connection.CreateCommand();
                command.CommandText = "SELECT * FROM ACCOUNT";

                H2DataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine($"USER_NAME: {reader["USER_NAME"]}  MAIL_ADDRESS: {reader["MAIL_ADDRESS"]}");
                }
            }
        }
Example #3
0
        /// <summary>
        /// - Creates a table with one primary key
        /// - inserts a value
        /// - loads the table to a DataTable and checks it has the expected value
        /// - inserts another value, reloads and checks it's there
        /// - updates the previously inserted value, reloads and checks it has the expected updated value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection"></param>
        /// <param name="typeStr"></param>
        /// <param name="originalValue"></param>
        /// <param name="updatedValue"></param>
        /// <param name="insertedValue"></param>
        static void SimpleTest <T>(H2Connection connection, String typeStr, T originalValue, T updatedValue, T insertedValue)
        {
            var tn = "simple_test_" + typeStr.Replace('(', '_').Replace(')', '_');

            new H2Command("create table " + tn + " (value " + typeStr + " primary key)", connection).ExecuteNonQuery();
            var originalValueStr =
                (originalValue is string) ? "'" + originalValue + "'" :
                (originalValue is DateTime) ? "parsedatetime('" + ((DateTime)(object)originalValue).ToString(CultureInfo.InvariantCulture) + "', 'MM/dd/yyyy hh:mm:ss')" :
                originalValue.ToString();

            new H2Command("insert into " + tn + " values (convert(" + originalValueStr + ", " + typeStr + "))", connection).ExecuteNonQuery();

            var adapter = new H2DataAdapter("select * from " + tn + " order by value", connection);

            new H2CommandBuilder(adapter);
            var table = new DataTable(tn)
            {
                CaseSensitive = false
            };

            adapter.Fill(table);

            Debug.Assert(table.Rows.Count.Equals(1));
            CheckVal(table.Rows[0][0], originalValue);

            table.Rows.Add(new object[] { insertedValue });
            adapter.Update(table);

            Reload(table, adapter);
            Debug.Assert(table.Rows.Count.Equals(2));
            CheckVal(table.Rows[1][0], insertedValue);

            table.Rows[1][0] = updatedValue;
            adapter.Update(table);

            Reload(table, adapter);
            Debug.Assert(table.Rows.Count.Equals(2));
            CheckVal(table.Rows[1][0], updatedValue);
        }
Example #4
0
 /// <summary>
 /// This method is called when the aggregate function is used.
 /// A new object is created for each invocation.
 /// </summary>
 /// <param name="connection">conn a connection to the database</param>
 /// <returns></returns>
 protected abstract int OnInit(H2Connection connection);
Example #5
0
 void org.h2.api.AggregateFunction.init(java.sql.Connection conn)
 {
     H2Connection connection = new H2Connection(conn);
     OnInit(connection);
 }
Example #6
0
        private void wizardPage3_ShowFromNext(object sender, EventArgs e)
        {
            /*Try Connect*/
            IDbConnection conn = null;

            try
            {
                if (comboBox1.SelectedIndex == 0 && !System.IO.File.Exists(txtOlaDb.Text.Replace(".h2.", ".lock.")))
                {
                    MessageBox.Show("OLA Does not seem to be started on server?\r\nPlease make sure OLA is running with connected clients when connecting, else all traffic will be redirected through this computer!");
                }
                conn = GetDBConnection(lstDB.SelectedItem as string);
                conn.Open();

                IDbCommand cmd = conn.CreateCommand();
                cmbOLAComp.Items.Clear();

                cmd.CommandText = "SELECT VersionNumber FROM Version WHERE moduleId = 1";
                try
                {
                    object res = cmd.ExecuteScalar();
                }
                catch (Exception ee)
                {
                    if (ee.Message.ToUpper().Contains("ENOUGH RIGHTS"))
                    {
                        conn.Close();
                        conn = new H2Connection("jdbc:h2://" + txtOlaDb.Text.Replace(".h2.db", "") + ";AUTO_SERVER=TRUE", "root", "");
                        try
                        {
                            conn.Open();
                            cmd = conn.CreateCommand();
                            TryApplyReadRights(cmd);
                        }
                        finally
                        {
                            conn.Close();
                        }
                        conn = GetDBConnection(lstDB.SelectedItem as string);
                        conn.Open();
                        cmd = conn.CreateCommand();
                    }
                }


                cmd.CommandText = "select eventid, name from Events";


                IDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    OlaComp cmp = new OlaComp();
                    cmp.Id   = Convert.ToInt32(reader["eventid"].ToString());
                    cmp.Name = Convert.ToString(reader["name"]);
                    cmbOLAComp.Items.Add(cmp);
                }
                reader.Close();
                cmd.Dispose();

                if (cmbOLAComp.Items.Count > 0)
                {
                    cmbOLAComp.SelectedIndex = 0;
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, ee.Message);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
Example #7
0
 /// <summary>
 /// This method is called by the database engine once when initializing the trigger.
 /// </summary>
 /// <param name="connection">a connection to the database</param>
 /// <param name="schemaName">the name of the schema</param>
 /// <param name="triggerName">the name of the trigger used in the CREATE TRIGGER statement</param>
 /// <param name="tableName">the name of the table</param>
 /// <param name="before">whether the fire method is called before or after the operation is performed</param>
 /// <param name="type">the operation type: INSERT, UPDATE, or DELETE</param>
 protected abstract void OnInit(H2Connection connection, String schemaName, String triggerName, String tableName, bool before, TriggerTypes type);
Example #8
0
        void org.h2.api.Trigger.init(java.sql.Connection conn, String schemaName, String triggerName, String tableName, bool before, int type)
        {
            H2Connection connection = new H2Connection(conn);

            OnInit(connection, schemaName, triggerName, tableName, before, (TriggerTypes)type);
        }
Example #9
0
 /// <summary>
 /// This method is called for each triggered action.
 /// </summary>
 /// <param name="connection"> a connection to the database</param>
 /// <param name="oldRow">the old row, or null if no old row is available (for INSERT)</param>
 /// <param name="newRow">the new row, or null if no new row is available (for DELETE)</param>
 /// <remarks>throw SQLException if the operation must be undone</remarks>
 protected abstract void OnFire(H2Connection connection, object[] oldRow, object[] newRow);
Example #10
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var connection = new H2Connection("jdbc:h2:mem:test");

            connection.Open();
            new H2Command("create table list (item integer primary key, description varchar(256), value integer)", connection).ExecuteNonQuery();
            new H2Command("insert into list values (1, 'First Item', 10)", connection).ExecuteNonQuery();
            new H2Command("insert into list values (2, 'Second Item', 11)", connection).ExecuteNonQuery();

            var table = new DataTable("test")
            {
                CaseSensitive = false
            };
            var adapter = new H2DataAdapter("select item, description, value from list order by item", connection);

            new H2CommandBuilder(adapter);
            adapter.Fill(table);

            table.Rows[1][1] = "First item modified";
            table.Rows[1][2] = 12;
            table.Rows.Add(new object[] { 3, "Third", 15 });
            adapter.Update(table);

            var table2 = new DataTable("test")
            {
                CaseSensitive = false
            };

            adapter.Fill(table2);

            var x1 = table.ToXml();
            var x2 = table2.ToXml();

            Debug.Assert(x1.Equals(x2));

            H2DataReader reader      = adapter.SelectCommand.ExecuteReader();
            int          readerCount = 0;

            Debug.Assert(reader.GetOrdinal("item") == 0);
            Debug.Assert(reader.GetOrdinal("Description") == 1);
            Debug.Assert(reader.GetOrdinal("VALUE") == 2);

            try
            {
                reader.GetOrdinal("throwException");
                Debug.Fail("Exception not thrown for GetOrdinal");
            }
            catch (IndexOutOfRangeException)
            {
            }

            while (reader.Read())
            {
                int    item        = reader.GetInt32(0);
                string description = reader.GetString(1);
                int    value       = reader.GetInt32(2);

                readerCount++;
                Debug.Assert(item == readerCount);
                CheckVal(reader["ITEM"], readerCount);

                switch (item)
                {
                case 1:
                    Debug.Assert(description == "First Item");
                    Debug.Assert(value == 10);
                    CheckVal(reader["description"], "First Item");
                    CheckVal(reader["Value"], 10);
                    break;

                case 2:
                    Debug.Assert(description == "First item modified");
                    Debug.Assert(value == 12);
                    CheckVal(reader["description"], "First item modified");
                    CheckVal(reader["Value"], 12);
                    break;

                case 3:
                    Debug.Assert(description == "Third");
                    Debug.Assert(value == 15);
                    CheckVal(reader["description"], "Third");
                    CheckVal(reader["Value"], 15);
                    break;

                default:
                    Debug.Fail("Unexpected item");
                    break;
                }
            }

            Debug.Assert(readerCount == 3);

            var count = new H2Command("select count(*) from list", connection).ExecuteScalar();

            Debug.Assert(((long)count).Equals(3));

            var one = new H2Command("select 1 from dual", connection).ExecuteScalar();

            Debug.Assert(((int)one).Equals(1));

            var a = new H2Command("select 'a' from dual", connection).ExecuteScalar();

            Debug.Assert(((string)a).Equals("a"));

            SimpleTest(connection, "int", 10, 11, 12);
            SimpleTest(connection, "bigint", (long)10, (long)11, (long)12);
            SimpleTest(connection, "smallint", (short)10, (short)11, (short)12);
            SimpleTest(connection, "tinyint", (byte)10, (byte)11, (byte)12);
            SimpleTest(connection, "double", 10d, 11d, 12d);
            SimpleTest(connection, "float", 10d, 11d, 12d); // double is float !
            SimpleTest(connection, "varchar(255)", "10", "11", "12");
            SimpleTest(connection, "timestamp", DateTime.Today, DateTime.Today.AddDays(1), DateTime.Today.AddDays(2));
            SimpleTest(connection, "date", DateTime.Today, DateTime.Today.AddDays(1), DateTime.Today.AddDays(2));
            //var x1 = table.ToXml();
        }
Example #11
0
 /// <summary>
 /// This method is called for each triggered action.
 /// </summary>
 /// <param name="connection"> a connection to the database</param>
 /// <param name="oldRow">the old row, or null if no old row is available (for INSERT)</param>
 /// <param name="newRow">the new row, or null if no new row is available (for DELETE)</param>
 /// <remarks>throw SQLException if the operation must be undone</remarks>
 protected abstract void OnFire(H2Connection connection, object[] oldRow, object[] newRow);
Example #12
0
 void org.h2.api.Trigger.init(java.sql.Connection conn, String schemaName, String triggerName, String tableName, bool before, int type)
 {
     H2Connection connection = new H2Connection(conn);
     OnInit(connection, schemaName, triggerName, tableName, before, (TriggerTypes)type);
 }
Example #13
0
 void org.h2.api.Trigger.fire(java.sql.Connection conn, object[] oldRow, object[] newRow)
 {
     H2Connection connection = new H2Connection(conn);
     OnFire(connection, oldRow, newRow);
 }
Example #14
0
 /// <summary>
 /// This method is called by the database engine once when initializing the trigger.
 /// </summary>
 /// <param name="connection">a connection to the database</param>
 /// <param name="schemaName">the name of the schema</param>
 /// <param name="triggerName">the name of the trigger used in the CREATE TRIGGER statement</param>
 /// <param name="tableName">the name of the table</param>
 /// <param name="before">whether the fire method is called before or after the operation is performed</param>
 /// <param name="type">the operation type: INSERT, UPDATE, or DELETE</param>
 protected abstract void OnInit(H2Connection connection, String schemaName, String triggerName, String tableName, bool before, TriggerTypes type);
        void org.h2.api.AggregateFunction.init(java.sql.Connection conn)
        {
            H2Connection connection = new H2Connection(conn);

            OnInit(connection);
        }
 /// <summary>
 /// This method is called when the aggregate function is used.
 /// A new object is created for each invocation.
 /// </summary>
 /// <param name="connection">conn a connection to the database</param>
 /// <returns></returns>
 protected abstract int OnInit(H2Connection connection);
Example #17
0
        void org.h2.api.Trigger.fire(java.sql.Connection conn, object[] oldRow, object[] newRow)
        {
            H2Connection connection = new H2Connection(conn);

            OnFire(connection, oldRow, newRow);
        }