Giorgi / DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
https://duckdb.net
MIT License
356 stars 62 forks source link

Reading DateTime has issue. #24

Closed RPalejiya closed 2 years ago

RPalejiya commented 2 years ago

Hello,

If I take the current Dev repo and try to select Datetime column. I get following Error.

System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.' This exception was originally thrown at this call stack: DuckDB.NET.Windows.WindowsBindNativeMethods.DuckDBValueInt32(DuckDB.NET.DuckDBResult, long, long) in NativeMethods.Windows.cs DuckDB.NET.Data.DuckDBDataReader.GetInt32(int) in DuckDBDataReader.cs DuckDB.NET.Samples.Program.PrintQueryResults(System.Data.Common.DbDataReader) in Program.cs DuckDB.NET.Samples.Program.AdoNetSamples() in Program.cs DuckDB.NET.Samples.Program.Main(string[]) in Program.cs

Here is my Sample Project snippet to reproduce it.

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 test2 (id Int, dt TIMESTAMP, string varchar  );";
            var executeNonQuery = command.ExecuteNonQuery();

            command.CommandText = "Insert into test2 values(1,'1992-09-20 11:30:00', 'Xyz');";
            executeNonQuery = command.ExecuteNonQuery();

            //command.CommandText = "select * from test2;";
            //var executeScalar = command.ExecuteScalar();

            command.CommandText = "select * from test2;";
            var reader = command.ExecuteReader();
            PrintQueryResults(reader);

            var results = duckDBConnection.Query<FooBar>("SELECT foo, bar FROM integers");

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

@RPalejiya If your column is Timestamp, you should not be calling GetInt32. You can call GetValue that works for any type or call one of the specialized GetXXX methods. Please see the updated demo.