Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Error al ejecutar consulta de inserción (https://www.clubdelphi.com/foros/showthread.php?t=92232)

LIGERO 01-09-2017 19:48:54

Error al ejecutar consulta de inserción
 
Buenas tarde:

Necesito hacer los siguiente

Código SQL [-]
Select @CodigoFoto = coalesce(MAX(codigo),0)+1 FROM CMRC_FOTOS;
Select @CodigoArticulo = CODARTICULO FROM ARTICULOS WHERE REFPROVEEDOR like :REFDELPROVEEDOR;

INSERT INTO CMRC_fotos values (@CodigoFoto,:FOTO,DEFAULT);

INSERT INTO CMRC_FOTOSARTICULOS (CODARTICULO,POSICION, ORDEN, PORTADA, CODFOTO, VERSION) 
VALUES (@CodigoArticulo,1,1,'T',@CodigoFoto,DEFAULT);


UPDATE ARTICULOS SET VISIBLEWEB=:VisibleEnWeb WHERE CODARTICULO=@CodigoArticulo;

Donde obtengo valores de unas tablas, para posteriormente a través de los parámetros REFDELPROVEEDOR, FOTO Y VisibleEnWeb, realizar las inserción de un registro con una foto y asignarle dicha foto a un artículo.

Hasta ahí, creo que la consulta no está mal hecha.

El problema es que no consigo que me funcione.
Estoy utilizando un SQLQuery y para mi base de datos utilizo SQLServer.

Aquí pongo el código de la inserción de una imagen.

Código:

procedure TForm1.CargarImagen3(directorio: string; indice: integer; visibleWEB : boolean);
var fichero: string;
    Imagen : TBitMap;
    Buffer : TStream;
begin

  Imagen := TBitmap.Create;
  Buffer := TMemoryStream.Create;

  fichero := directorio+'\'+FileListBox1.Items.Strings[indice];

  if (ANSIUpperCase(ExtractFileExt(fichero)) = '.JPG')
      or (ANSIUpperCase(ExtractFileExt(fichero)) = '.JPEG') then
  begin
    Imagen := ConvertJPG2BMP(fichero);
    Imagen.SaveToStream(buffer);
    DM.SQLQuery2.Params.ParamByName('FOTO').LoadFromStream(buffer,ftblob);
  end
  else
    DM.SQLQuery2.Params.ParamByName('FOTO').LoadFromFile(fichero,ftblob);


  DM.SQLQuery2.Params.ParamValues['REFDELPROVEEDOR'] := NombreSinExtension(FileListBox1.Items.Strings[indice]);



  DM.SQLQuery2.Params.ParamValues['VISIBLEENWEB'] := visibleWeb;

  DM.AplicarCambios(DM.SQLConnection1, DM.ClientDataSet2, false);


end;

Y el código de AplicarCambios es

Código:

procedure TDM.AplicarCambios(SQLConnection : TSQLConnection; ClientDataSet : TClientDataSet; seleccion : Boolean);
begin
  SQLConnection.Close;
  SQLConnection.Open;

  if seleccion = true then
    ClientDataSet.Open
  else
    ClientDataSet.Execute;

  if not (ClientDataSet.State = dsInsert) then
    ClientDataSet.Edit;

  ClientDataSet.Post;
  if (ClientDataSet.ChangeCount > 0) then
        ClientDataSet.ApplyUpdates(-1);
  ClientDataSet.Close;
  ClientDataSet.Open;
end;

En la línea
ClientDataSet.Edit;

me da el error "cannot perform this operation on a closed dataset"

He buscado por todos los lados, pero no encuentro nada que me solucione el problema.

Espero que podáis ayudarme.
Gracias.

oscarac 01-09-2017 20:26:33

y si lo haces de esta manera?


Código Delphi [-]
if not (ClientDataSet.State = dsInsert) then
Begin
    ClientDataSet.Open;
    ClientDataSet.Edit;
End;

LIGERO 04-09-2017 14:02:56

Gracias Oscar por contestar.

Si pongo esta sentencia me sale el error

SQLQuery2: Cursor not retorned from Query

ya que al hacer el open entiende que la consulta es de tipo SELECT y espera un conjunto de registros, pero la sentencia es de tipo INSERT y UPDATE.

LIGERO 05-09-2017 18:16:15

Bueno, después de romperme la cabeza, la solución era mucho mas sencilla de lo que parecía.
Aquí pongo el código para el que quiera verlo.
Gracias a todos
Código Delphi [-]
procedure TForm1.CargarImagen3(directorio: string; indice: integer; visibleWEB : boolean);
var fichero: string;
    Imagen : TBitMap;
    Buffer : TStream;
    CodigoFoto, CodigoArticulo : integer;
    Visible : string;
begin
  Imagen := TBitmap.Create;
  Buffer := TMemoryStream.Create;
  Visible := IfThen(VisibleWeb,'T','F');
  fichero := directorio+'\'+FileListBox1.Items.Strings[indice];

  if (ANSIUpperCase(ExtractFileExt(fichero)) = '.JPG')
      or (ANSIUpperCase(ExtractFileExt(fichero)) = '.JPEG') then
  begin
    Imagen := ConvertJPG2BMP(fichero);
    Imagen.SaveToStream(buffer);
    DM.SQLQuery2.Params.ParamByName('FOTO').LoadFromStream(buffer,ftblob);
  end
  else
    DM.SQLQuery2.Params.ParamByName('FOTO').LoadFromFile(fichero,ftblob);

  DM.SQLQuery2.Params.ParamValues['REFDELPROVEEDOR'] := NombreSinExtension(FileListBox1.Items.Strings[indice]);

  DM.SQLQuery2.Params.ParamValues['VISIBLEENWEB'] := visible;

  DM.ClientDataSet2.Close;
  DM.ClientDataSet2.Execute;

end;
Como podéis ver era tan fácil como asignar lo parámetros en el SQLQuery2 y cerrar y ejecutar la sentencia en el ClientDataSet2


La franja horaria es GMT +2. Ahora son las 20:05:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi