Save new file or Replace existing file SQL C # .net

0

I'm with a project that stores documents, My question is how can I store them and that SQL tell me if the document with the same name or ID exists.

This is the SQL code:

USE [HConDe]
GO
/****** Object:  StoredProcedure [dbo].[ActualizarDocumento]    Script Date: 11/09/2018 10:54:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[ActualizarDocumento]

@iddocu int, @nomdocu varchar(50), @fechadocu date, @contdocu varchar(max)


as 

--ActualizarDocumento

if not exists (SELECT id_documento from Documento where id_documento=@iddocu)

insert into Documento (id_documento,nombre_documento,fecha_documento,contenido_documento) values (@iddocu, @nomdocu , @fechadocu, @contdocu)

else print 'Archivo existe'

In C # I execute it. The problem is that I replaced the file without any dialogue before. I wanted to know how I could do it ..

public override bool Guardar()          
{
    try {

        string cmd = String.Format("EXEC ActualizarDocumento '{0}','{1}','{2}','{3}'", txtiddocu.Text.Trim(), txtDocu.Text.Trim(), dt1.Value.ToString(), rich.Trim());
        utlidades.Ejecutar(cmd);
        MessageBox.Show("Se ha guardado correctamente");
        return true;

    }
    catch(Exception error) {

        MessageBox.Show("Se ha producido un error: " + error.Message);
        return false;

    }
}
    
asked by Ibarra Emiliano 11.09.2018 в 15:58
source

1 answer

0

Your SP is not doing any UPDATE when the file exists, this is the solution:

IF NOT EXISTS (SELECT id_documento FROM Documento WHERE id_documento=@iddocu)
    INSERT INTO Documento (id_documento,nombre_documento,fecha_documento,contenido_documento) 
    VALUES (@iddocu, @nomdocu , @fechadocu, @contdocu)
ELSE
    UPDATE Documento SET 
        nombre_documento = @nomdocu, 
        fecha_documento = @fechadocu, 
        contenido_documento = @contdocu 
    WHERE id_documento=@iddocu
    
answered by 14.09.2018 в 00:47