【问题标题】:Error Retrieving when saving or retrieving a BD SQL Server image保存或检索 BD SQL Server 映像时检索错误
【发布时间】:2017-06-02 02:57:25
【问题描述】:

将图像保存到 SQL Server 2014 数据库时,保存在 Image 类型的列中。问题是,当检索图像并以变量类型byte[] 接收图像时,我无法在预期的控制中看到正确的转换,我已经确定将其发送到数据库时字节数为 139551 字节,但是从数据库中检索它时,字节数为 13 个字节 [13]。显然无法将列类型更改为varbinary (MAX),因为它只支持8000字节...

数据库恢复:

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

Session["Consulta"] = Tabla;

处理程序文件:

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

感谢您的帮助!

【问题讨论】:

  • image 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这种数据类型,并计划修改当前使用它们的应用程序。请改用 varbinary(max)(它支持 20 亿字节 (2 GB) 的大小 - NOT 只有 8000 字节!See details here

标签: c# html asp.net sql-server image


【解决方案1】:

已解决,将数据库图像字段中的数据类型更改为varbinary(MAX),并使用定义SqlDbType.VarBinary的存储过程将图像插入数据库,这样过程中我没有丢失字节,之前我只是在Image字段中保存了变量类型[]字节。

表格:

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

错误:

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

已解决:

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

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2012-03-03
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2014-08-22
    相关资源
    最近更新 更多