0

I'm trying to execute a query that will get me a value from my postgre database using c# with the npgsql plugin. I got the query from here.

https://dba.stackexchange.com/a/90567

This is the query

string query = String.Format(@"SELECT a.attrelid::regclass::text, a.attname
                                             , CASE a.atttypid
                                                 WHEN 'int'::regtype  THEN 'serial'
                                                 WHEN 'int8'::regtype THEN 'bigserial'
                                                 WHEN 'int2'::regtype THEN 'smallserial'
                                               END AS serial_type
                                        FROM   pg_attribute  a
                                        JOIN   pg_constraint c ON c.conrelid  = a.attrelid
                                                              AND c.conkey[1] = a.attnum 
                                        JOIN   pg_attrdef   ad ON ad.adrelid  = a.attrelid
                                                              AND ad.adnum    = a.attnum
                                        WHERE  a.attrelid = '""public.{0}""'::regclass            
                                        AND    a.attnum > 0
                                        AND    NOT a.attisdropped
                                        AND    a.atttypid = ANY('{int,int8,int2}'::regtype[])
                                        AND    array_length(c.conkey, 1) = 1   
                                        AND    ad.adsrc = 'nextval('''
                                                    || (pg_get_serial_sequence (a.attrelid::regclass::text, a.attname))::regclass
                                                    || '''::regclass)';  ", record);

It's giving me a

Input string was not in a correct format.

When it goes through that variable. I only have 1 variable and that's the {0}, but I don't know why it's yelling at me with that.

EDIT:

Btw, the double quotes(") are needed because it is created using entity framework and that's just how it works when you execute the query on pgAdminIII. All the tables needs to be inside them.

3
  • 1
    What about {int,int8,int2}? Try {{int,int8,int2}}. Commented Jun 23, 2015 at 4:52
  • 1
    In the original ErwinBrandstetter version, the where clause is WHERE a.attrelid = 'tbl'::regclass : only single quotes around the table name, since it is a string literal there. You probably need something like quote_literal( ''{0}'' ) to parametrize your query. Commented Jun 23, 2015 at 9:49
  • @glenebob , you were right! that worked Commented Jul 2, 2015 at 4:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.