简体   繁体   English

保存或检索BD SQL Server映像时检索错误

[英]Error Retrieving when saving or retrieving a BD SQL Server image

When saving an image to the SQL Server 2014 database, is saved in a column of type Image . 将图像保存到SQL Server 2014数据库时,将保存在类型为Image的列中。 The problem is that when retrieving the image and receive it in a variable type byte[] doing the right conversion I can not see in the control intended, I have identified that when sending it to the database the number of bytes is 139551 bytes, but when retrieving it from the database, the number of bytes is 13 bytes [13]. 问题是,当检索图像并将其以可变类型byte[]进行正确转换时,我无法在预期的控件中看到该图像,我已经确定,将其发送到数据库时,字节数为139551字节,但是从数据库中检索时,字节数为13个字节[13]。 Obviously could not change the column type to varbinary (MAX) because it only supports 8000 bytes... 显然无法将列类型更改为varbinary (MAX)因为它仅支持8000个字节...

Database 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 ! 我感谢您的帮助 !

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. 解决后,将“数据库映像”字段中的数据类型更改为varbinary(MAX),并使用定义SqlDbType.VarBinary的存储过程将映像插入数据库,以便在保存变量之前,我不会在进程中丢失字节变量类型[图片字段中的[]个字节。

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;
        }

Solved: 解决了:

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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM