天天看點

PostgreSQL中表和字元串大寫的問題

在PostgreSQL中,如果表名或者字段名中存在大寫字元,這個sql執行就會錯誤。

解決方法:

  給帶有大寫字母的表名或者字段名加上引号。

  eg:

    "Employee"

    "Name"

這種情況在用戶端程式設計也一樣,如果是C#用戶端,也必須加上引号。

 我們建立一個如下的表:

CREATE TABLE "Employee"

(

  "Name" character varying(20),

  "Age" integer,

  "Id" serial NOT NULL

)

WITH (OIDS=FALSE);

ALTER TABLE "Employee" OWNER TO postgres;

 C#用戶端代碼如下:

protected void btnInsert_Click(object sender, EventArgs e)

       {

            string sql = "insert into \"Employee\"(\"Name\", \"Age\") "

                       + "values('" + txtName.Text + "', " + txtAge.Text + ")";

            //string sql = "insert into Employee(Name, Age) "

            //           + "values('" + txtName.Text + "', " + txtAge.Text + ")";

            PostSqlUtil db = new PostSqlUtil();

            if (db.ExecuteSQL(sql) > 0)

                Response.Write("一條記錄插入成功!");

            else

                Response.Write("記錄插入失敗!");

        }