c#

时间:2016-12-17 22:31:49

标签: c# postgresql npgsql

我有cmd.ExecuteReader()的问题我得到一个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);

        }


    }

我在下面的代码中获得了Exeption:

  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();
    }

我的数据库名为DoveVer3,我的架构DoveSchema在这里是我的表代码:

enter image description here

    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;

Exceptions Base messege:

  

关系“dovetable”不存在; Statemants:{SELECT * FROM DoveTable   在哪里doveId ='测试'}

1 个答案:

答案 0 :(得分:0)

默认情况下,传递给PostgreSQL的所有标识符都将被视为小写。根据您的创建脚本,您在定义表时使用quoted-identifer,因此在使用表名时需要以下内容。

SELECT * FROM "DoveTable"

请注意,它用双引号括起来,'D'和'T'都是大写的。使用带引号的标识符时,必须始终按照定义它们的方式将它们写出来。