Exemplo n.º 1
0
        private static void AdoNetSamples()
        {
            if (File.Exists("file.db"))
            {
                File.Delete("file.db");
            }

            using (var duckDBConnection = new DuckDBConnection("Data Source=file.db"))
            {
                duckDBConnection.Open();

                var command = duckDBConnection.CreateCommand();

                command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);";
                var executeNonQuery = command.ExecuteNonQuery();

                command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);";
                executeNonQuery     = command.ExecuteNonQuery();

                command.CommandText = "Select count(*) from integers";
                var executeScalar = command.ExecuteScalar();

                command.CommandText = "SELECT foo, bar FROM integers";
                var reader = command.ExecuteReader();

                PrintQueryResults(reader);
            }
        }
Exemplo n.º 2
0
 public void ShouldOpenAndCloseInMemoryConnection()
 {
     using (var duckDBConnection = new DuckDBConnection())
     {
         duckDBConnection.Open();
         duckDBConnection.Close();
     }
 }
Exemplo n.º 3
0
        private static void AdoNetSamples()
        {
            if (File.Exists("file.db"))
            {
                File.Delete("file.db");
            }
            var duckDb = new DuckDb("Data Source=file.db");

            using var duckDBConnection = new DuckDBConnection(duckDb);
            duckDBConnection.Open();

            using var conn2 = new DuckDBConnection(duckDb);
            conn2.Open();

            var command = duckDBConnection.CreateCommand();

            command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);";
            var executeNonQuery = command.ExecuteNonQuery();



            command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);";
            executeNonQuery     = command.ExecuteNonQuery();

            command.CommandText = "Select count(*) from integers";
            var executeScalar = command.ExecuteScalar();

            var command2 = duckDBConnection.CreateCommand();

            command2.CommandText = "SELECT foo, bar FROM integers";
            var reader = command2.ExecuteReader();

            PrintQueryResults(reader);

            try
            {
                command.CommandText = "Not a valid Sql statement";
                var causesError = command.ExecuteNonQuery();
            }
            catch (DuckDBException e)
            {
                Console.WriteLine(e.Message);
            }

            //sending a dapper queury
            duckDBConnection.Execute("CREATE TABLE integers2(foo INTEGER, bar INTEGER);");
        }
Exemplo n.º 4
0
        public void ShouldReadSimpleTable()
        {
            using (var duckDBConnection = new DuckDBConnection())
            {
                duckDBConnection.Open();

                var createTableCommand = duckDBConnection.CreateCommand();

                createTableCommand.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);";
                var executeNonQuery = createTableCommand.ExecuteNonQuery();

                var insertValuesCommand = duckDBConnection.CreateCommand();
                insertValuesCommand.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);";
                executeNonQuery = insertValuesCommand.ExecuteNonQuery();

                var readValuesCommand = duckDBConnection.CreateCommand();
                readValuesCommand.CommandText = "SELECT foo, bar FROM integers;";
                var reader = readValuesCommand.ExecuteReader();

                reader.HasRows.Should().BeTrue();

                var results = new List <Tuple <int?, int?> >();

                while (reader.Read())
                {
                    var foo = reader.GetInt32(0);
                    var bar = reader.GetInt32(1);

                    results.Add(new Tuple <int?, int?>(foo, bar));
                }

                results.Count.Should().Be(3);

                results[0].Item1.Should().Be(3);
                results[0].Item2.Should().Be(4);

                results[1].Item1.Should().Be(5);
                results[1].Item2.Should().Be(6);

                results[2].Item1.Should().Be(7);
                results[2].Item2.Should().Be(null);
            }
        }