0

I have an application written in C# using VS 2012 that needs to determine the type of data contained in the result of a PostgreSQL query at run time. For example, a command object is set up to execute "select * from my_table". For each field in the returned reader, a line is written into a file, which would end up looking like this:

my_table.field1 is an integer
my_table.field2 is a real
my_table.field3 is a timestamp

The problem comes with fields that contain intervals. Using both the latest PostgreSQL ODBC driver and NPGSQL, a field that contains an interval is read as though it contained text. I can use the TimeSpan.Parse() method to see if the data in the field can be mapped to a TimeSpan, but that feels like a violation of a basic database principle to me. Is there a way for a C# application to directly determine that a field contained in a DataReader object is of type interval?

ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57
RobR
  • 1
  • I'm affraid you are choosing the wrong language to do this. C# is strong typed lang, so it supose to "know" the type of field returned from a query in advance. Using something like "SELECT *" on c# is violating that principle. That is what i have been told when i ask questions like this – ericpap Dec 10 '14 at 15:50
  • My application generates wrapper code that builds classes in C# or C++ for use in other applications my company writes. So, I need to be able to tell the difference between intervals and plain ordinary text fields. And yes, I know there are things like Entity Framework that will do this kind of thing, but we already have wrapper classes that were written by hand, and my tool follows the patterns established for them. – ROBERT RICHARDSON Dec 10 '14 at 16:00
  • Here's a question and answer that gives me an alternate way of doing what I want to do: http://stackoverflow.com/questions/15928118/how-to-get-column-attributes-query-from-table-name-using-postgresql The only problem with that is that it works only with structures (tables or views) that are already defined. The existing version of my tool works that way, and I will probably modify it to use this technique. However, I'd like to be able to determine field types from arbitrary queries. – ROBERT RICHARDSON Dec 10 '14 at 20:54
  • Sounds like a driver limitation in nPgSQL; it should ideally return instances of a custom class for PostgreSQL interval types. – Craig Ringer Dec 11 '14 at 03:43

1 Answers1

0

Very late to this, but you can always use DbDataReader.GetTypeName() to get the name of the database type.

Shay Rojansky
  • 15,357
  • 2
  • 40
  • 69