简体   繁体   中英

Postgresql Npgsql.PostgresException in c#

I have a problem with cmd.ExecuteReader() i get a Npgsql.PostgresException

    public void connectDB()
    {
        try
        {
            server = "localhost";
            database = "DoveVer3";
            uid = "admin";
            password = "admin";
            string connectionString;
            connectionString = "Host=" + server + ";Username =" + uid + ";" + "PASSWORD=" + password + ";DATABASE=" + database;

            connection.ConnectionString = connectionString;
            connection.Open();

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }


    }

I get the Exeption in the code below:

  public void AddDoveToDB(Dove dove)
    {
        //add new dove record to tableDB
        connectDB();
        cmd = new NpgsqlCommand();
        cmd.Connection = connection;
        cmd.CommandText = "SELECT * FROM " + DoveTableDB + " WHERE `" + DoveIdColumnDoveTable + "` = '" + dove.GetDoveId() + "'";
        NpgsqlDataReader rdr = cmd.ExecuteReader(); //// <<<<< HERE
        if (rdr.Read() != true)
        {
            rdr.Close();
            cmd.Parameters.Clear();

            cmd.CommandText = "INSERT INTO " + DoveTableDB + "(" + DoveIdColumnDoveTable + "," + DoveIdFatherColumnDoveTable + "," + DoveIdMotherColumnDoveTable + "," + DoveEyesColorColumnDoveTable + "," + DoveFeatherColorDoveTable + "," + DoveImageNameColumnDoveTable + "," + DoveSexColumnDoveTable +") VALUES ('" + dove.GetDoveId() + "','" + dove.GetDoveFatherId() + "','" + dove.GetDoveMotherId() + "','" + dove.GetEyesColor() + "','" + dove.GetFeathersColor()+ "','" + dove.GetImageName() + "','" + dove.GetSex()+ "')";
            cmd.ExecuteNonQuery();

        }
        connection.Close();
    }

My database is called DoveVer3 and my schema DoveSchema here is my table code:

在此输入图像描述

    Name: DoveTable; Type: TABLE; Schema: DoveSchema; Owner: admin
--

CREATE TABLE "DoveTable" (
    "doveId" character varying(20)[] NOT NULL,
    "doveFather" character varying(20)[],
    "doveMother" character varying,
    "doveEyesColor" character varying(20)[],
    "doveFeathersColor" character varying(20)[],
    "doveSex" smallint DEFAULT 3 NOT NULL,
    "imageName" character varying(30)
);
ALTER TABLE "DoveTable" OWNER TO admin;

The Exceptions Base messege:

relation "dovetable" don't exist; Statemants: {SELECT * FROM DoveTable WHERE doveId = 'Test'}

By default all identifiers passed to PostgreSQL will be considered lower-case. Based on your creation script you used a quoted-identifer when defining your table so the following is required when using the name of your table.

SELECT * FROM "DoveTable"

Notice that it is wrapped in double quotes and the 'D' and 'T' are both capitalized. When using quoted-identifiers you must always write them out exactly the way you defined them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM