Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/09/2008, 01:24
chcma
 
Fecha de Ingreso: junio-2003
Ubicación: Asturias
Mensajes: 2.429
Antigüedad: 20 años, 11 meses
Puntos: 7
C# - Insertar Binary en SqlServer en campo de tipo Binary (No IMAGE)

Buenos días foreros,

Verán, tengo una tabla sencilla con un campo de tipo Binary(50) y en él quiero meter imágenes y otro tipo de ficheros. Pues bien, tengo el siguiente código el cual está correcto, pero cuando llamo al procedimiento almacenado me inserta el contenido, pero defectuoso... (0x410000000....)

Se que para las imágenes está el tipo IMAGE y con ese me lo hace bien, pero con ese no puedo hacer SELECT DISTINCT, además de que... las imágenes también son binarias. ¿No deberian insertarse correctamente también en este tipo de campo?

El código que tengo es el siguiente:

Código:
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] byteDatos = CargarBinario();
            if (byteDatos == null)
                return;

            SqlConnection objCn = new SqlConnection(@"Data Source=GSPROYECTO11\SQLEXPRESS;Initial Catalog=BdCamiones;Integrated Security=SSPI;");
            objCn.Open();

            //CREO COMANDO
            SqlCommand objCmd = new SqlCommand("PINSERTAR_BINARIO", objCn);
            objCmd.CommandType = CommandType.StoredProcedure;

            //CREO PARAMETROS
            SqlParameter objP1 = new SqlParameter("@IMAGEN_BINARIA", byteDatos);
            objCmd.Parameters.Add(objP1);

            //EJECUTO
            int numResultado = objCmd.ExecuteNonQuery();

            objCn.Close();
        }

        private byte[] CargarBinario()
        {
            OpenFileDialog opfDialogo = new OpenFileDialog();
            if(opfDialogo.ShowDialog() == DialogResult.OK)
            {
                FileStream fsStream = new FileStream(opfDialogo.FileName, FileMode.Open);
                BinaryReader brReader = new BinaryReader(fsStream);

                byte[] byteDatos = brReader.ReadBytes((int)fsStream.Length);
                brReader.Close();
                fsStream.Close();

                return byteDatos;
            }

            return null;
        }

En fin, espero puedan ayudarme. Desde ya, gracias por la ayuda ofrecida siempre por aqui.
__________________
Charlie.