/// <summary> /// Executes the select query. /// </summary> /// <param name="connectionString">The connection string.</param> /// <param name="query">The query.</param> /// <returns>A data table containing the result of the query</returns> public DataTable ExecuteSelectQuery(string connectionString, string query) { using (System.Data.H2.H2Connection connection = new System.Data.H2.H2Connection(connectionString, "sa", string.Empty)) { this.logger.LogVerbose(string.Format("Execute query {0}", query)); connection.Open(); var com = new H2Command(query, connection); var result = com.ExecuteReader(); var tableResult = new DataTable(); for (int i = 0; i < result.FieldCount; i++) { tableResult.Columns.Add(result.GetName(i), result.GetFieldType(i)); } while (result.Read()) { var row = tableResult.NewRow(); for (int i = 0; i < result.FieldCount; i++) { row[i] = result.GetValue(i); } tableResult.Rows.Add(row); } return(tableResult); } }
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(); }
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"]}"); } } }
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(); }