使用Postgres和C#插入Where

本文关键字:插入 Where Postgres 使用 | 更新日期: 2023-09-27 18:25:57

我一直在搜索,但我不知道如何搜索,而且我是新手,所以如果有人能帮助我,我会很感激的!

问题是,我需要在一个名为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键。这些表只有两列,id列和name列。在我的程序代码中,这些名称列在组合框中。我需要的是将所有选定的数据和名称等插入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都存储在数据库中,我用一个按字母顺序排序的查询来填充这些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" = '......

使用Postgres和C#插入Where

您正在寻找:

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

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