Error Recover when saving or retrieving a BD SQLServer image

0

When saving the Image in the database it is saved in a field of type Image the problem is that when recovering the image and receiving it in a variable type [] byte doing the correct conversion I can not visualize in the control destined, I have identified that when sending it to the database the number of bytes is 139551 byte [139551] , but when retrieving it from the database the number of bytes is 13 byte [13] obviously you could not change the field type to varbinary (MAX) for supporting only 8000 bytes ...

DB recovery

DataTable Tabla = ConsultasMasivasN.ConsultarSoporteIndicadorN(CodigoGI, UsuarioS);
                ViewState["TblSoporte"]=Tabla;
                gridListado.DataSource = Tabla;
                gridListado.DataBind();
                Session["Consulta"] = Tabla;

Handler File

if (context.Session["Consulta"] != null)
            {
                DataTable tbRegistro = (DataTable)context.Session["Consulta"];
                DataRow drRegistro = tbRegistro.Select(string.Format("Codigo={0}", context.Request.QueryString["Codigo"]))[0];
                byte[] imagen = (byte[])drRegistro["Soporte"];
                context.Response.ContentType = "image/jpg";
                context.Response.OutputStream.Write(imagen, 0, imagen.Length);
            }

I appreciate your help !!!!

    
asked by Enrique Hurtado 17.01.2017 в 15:08
source

1 answer

0

Resolved, Change the data type in the Database Image field to varbinary (MAX) and use a stored procedure defining SqlDbType.VarBinary to insert the image into the database so I did not lose bytes in the process, before I just saved The variable type [] byte in the Image field.

Table:

CREATE TABLE [dbo].[SoporteIndicador](
    [Codigo] [int] IDENTITY(1,1) NOT NULL,
    [CodigoIM] [varchar](50) NOT NULL,
    [CodigoGI] [varchar](50) NOT NULL,
    [Soporte] [varbinary](max) NOT NULL,
    [NombreSoporte] [varchar](150) NOT NULL,
    [UsuarioRegistro] [varchar](50) NOT NULL,
    [FechRegistro] [datetime] NOT NULL

Error:

public static int RegistrarSoporteIndicador(SoporteIndicador soporteIndicador)
        {
            int Ingreso = 0;
            using (SqlConnection conexion = Conexion.ObtenerConexion())
            {
                SqlCommand Ingresar = new SqlCommand(string.Format(
                    "Insert Into SoporteIndicador (CodigoIM,CodigoGI,Soporte,NombreSoporte,UsuarioRegistro,FechRegistro) values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')",
                soporteIndicador.CodigoIM,
                soporteIndicador.CodigoGI,
                soporteIndicador.Soporte, //Type []byte                soporteIndicador.NombreSoporte,
                soporteIndicador.UsuarioRegistro,
                soporteIndicador.FechRegistro), conexion);
                Ingreso = Ingresar.ExecuteNonQuery();
                conexion.Close();
            }
            SqlConnection cerrarcon = Conexion.CerrarConexion();
            return Ingreso;
        }

Solution:

public static int RegistrarSoporteIndicador(SoporteIndicador soporteIndicador)
        {
            int Ingreso = 0;
            using (SqlConnection conexion = Conexion.ObtenerConexion())
            {
                using (SqlCommand cmd = new SqlCommand("PA_Guardar_Registro", conexion))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("CodigoIM", soporteIndicador.CodigoIM);
                    cmd.Parameters.AddWithValue("CodigoGI", soporteIndicador.CodigoGI);
                    SqlParameter imageParam = cmd.Parameters.Add("@Soporte", System.Data.SqlDbType.VarBinary);
                    imageParam.Value = soporteIndicador.Soporte;
                    cmd.Parameters.AddWithValue("NombreSoporte", soporteIndicador.NombreSoporte);
                    cmd.Parameters.AddWithValue("UsuarioRegistro", soporteIndicador.UsuarioRegistro);
                    cmd.Parameters.AddWithValue("FechRegistro", soporteIndicador.FechRegistro);


                    Ingreso = cmd.ExecuteNonQuery();
                    conexion.Close();
                }
            }
            SqlConnection cerrarcon = Conexion.CerrarConexion();
            return Ingreso;
        }
    
answered by 18.01.2017 в 02:30