Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i use Quotes inside of Quotes in C# and not let it error

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'"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

string t = "SELECT * FROM \"Users\";";

or

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

should work

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading