Ver Mensaje Individual
  #1  
Antiguo 01-09-2017
LIGERO LIGERO is offline
Miembro
 
Registrado: jun 2007
Posts: 42
Reputación: 0
LIGERO Va por buen camino
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.
Responder Con Cita