插入使用Postgres和C#的位置

时间:2012-02-25 05:00:46

标签: c# postgresql

我一直在寻找,但我不知道怎么做,而且我是新手,所以如果有人可以帮助我,我会很感激!

问题是我需要在一个名为Farmaco的表中插入很多东西。 我的桌子Farmaco:

CREATE TABLE "Farmaco" (
  "idFarmaco" bigint NOT NULL DEFAULT nextval('idfarmaco'::regclass),
  "Nombre" character varying(100) NOT NULL,
  "PrecioReferencial" real NOT NULL,
  "Descripcion" character varying(500),
  "Stock" integer NOT NULL,
  "MinStock" integer,
  "MaxStock" integer,
  "idPresentacion" integer NOT NULL,
  "idTipo" integer NOT NULL,
  "idUnidadDeManejo" integer NOT NULL,
  "idMarca" integer NOT NULL,
  CONSTRAINT "PKFarmaco" PRIMARY KEY ("idFarmaco" ),
  CONSTRAINT "FKFarmaco_Marca" FOREIGN KEY ("idMarca")
      REFERENCES "Marca" ("idMarca") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_Presentacion" FOREIGN KEY ("idPresentacion")
      REFERENCES "Presentacion" ("idPresentacion") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_Tipo" FOREIGN KEY ("idTipo")
      REFERENCES "Tipo" ("idTipo") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_UnidadDeManejo" FOREIGN KEY ("idUnidadDeManejo")
      REFERENCES "UnidadDeManejo" ("idUnidadDeManejo") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "Farmaco"
  OWNER TO "Daniel";

除了idFarmaco之外的所有id都是其他表的Foreing Keys。这些表只有2列,id为1,名称为1。在我的程序代码中,这些名称列在组合框中。我需要的是将所有选定的数据和名称等插入farmaco。

例如

我想在表格中插入“Farmaco”:

"Nombre": Stack (textbox)
"PrecioReferencial": 150.30 (textbox)
"Descripcion" : spensive (textbox)
"Stock" : 20 (textbox)
"MinStock" : 10 (textbox)
"MaxStock" : 50 (textbox)
"idPresentacion" : Caja (this was selected in the combobox)
"idTipo" : Medicina (this was selected in the combobox)
"idUnidadDeManejo": Unidad (this was selected in the combobox)
"idMarca" : Bayer (this was selected in the combobox)

所有这些id都存储在数据库中,并且即时填充查询,按字母顺序排序,但现在我不知道如何进行查询,这样我就可以在texbox中插入所有文本的各自ID。

我得到的更远的是:

Insert into "Farmaco"
Select "idPresentacion", "idTipo", "idMarca", "idUnidadDeManejo"
From "Presentacion", "Tipo", "Marca", "UnidadDeManejo"
Where "NombrePresentacion" = 'Here should be the text in the ComboBox cbPresentacion.SelectedItem.ToString()' AND "NombreTipo" = '......

2 个答案:

答案 0 :(得分:1)

您在寻找:

insert into "Farmaco" ("idPresentacion", "idTipo", "idMarca", "idUnidadDeManejo")
values ( cbPresentacion.SelectedItem.ToString(), ... ) 

答案 1 :(得分:0)

假设您正在使用winforms项目:您可以为组合框提供数据源并设置displaymember和datamember。 displaymember是在组合框中显示为选项的成员,datamember是您在幕后使用的与所选显示成员对应的成员。在您的情况下:使用表Presentacion中的信息创建数据源,并将idPresentacion设置为datamember,将NamePresentacion设置为displaymember