skip to Main Content

Im trying to query some data from a postgres server using C#.

This is the code im currently working with:

    namespace WpfApp1
    {
        class postgresDB
        {

            public void Init_database()
            {

                NpgsqlConnection conn = new NpgsqlConnection("Server=FS01;User Id=postgres;" +
                                        "Password=pwd;Database=postgres;");
                conn.Open();
                Debug.WriteLine("DB opened");

                NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM 'Users';", conn);

                NpgsqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                    Console.Write("{0}n", dr[0]);

                conn.Close();
                Debug.WriteLine("DB closed");
            }

        }
    }

The Problem I have is that the SELECT query errors with this error:
42601: syntax error at or near "'Users'"

Im trying to do this in the first place because the postgres table name needs to be in quotes so it doesnt check the upper/lower case letters of the name. If i put the name in quotes it just skips that and finds the table anyways.

2

Answers


  1. You should be able to use "

    In most languages I came across is used as an escape character. You can use it to use quotes within quotes or alternatively add a new line with n a tab with t and many more things.

    Fun fact if you want to use within a string literal you need to use \

    Login or Signup to reply.
  2. string t = "SELECT * FROM "Users";";
    

    or

    string t2 = @"SELECT * FROM ""Users"";";
    

    should work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search