2017-09-12 1 views
1

Ich habe ein Problem mit dem Code C# unten, ich sollte ein Binärbild in einer SQL Server-Datenbank 2014 speichern, ich habe die Funktion, um das Bild in binäre konvertieren, das Bild wählen Sie mit eine Schaltfläche, das Problem ist, dass, wenn ich die Datenbank auf 0x00 im Feld Immagine speichern, wie kann ich diese Art von Fehler beheben? die Immagine Feldformat ist binärBinäre Bild in SqlServer 2014 speichern von C#

QUERY:

private void buttonCaricaImmagine_Click(object sender, EventArgs e) 
{    
    OpenFileDialog of = new OpenFileDialog(); 
    //For any other formats 

    of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG"; 
    if (of.ShowDialog() == DialogResult.OK) 
    { 

     pictureBoxRapportino.ImageLocation = of.FileName; 
     imm = pictureBoxRapportino.Image; 
     checkImage = 1; 
    } 
} 

public byte[] ImageToByteArray(System.Drawing.Image imageIn) 
{ 
    ImageConverter _imageConverter = new ImageConverter(); 
    byte[] xByte = (byte[])_imageConverter.ConvertTo(imageIn, typeof(byte[])); 
    return xByte; 
} 

private void buttonInserimento_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     if(checkImage==1 && textBoxNumeroDocumento.Text != "") 
     { 
      //controlla dati 
      int NumeroDocumento = int.Parse(textBoxNumeroDocumento.Text); 

      byte[] ImmagineBinaria = ImageToByteArray(imm); 
      string BinaryImageCast = Encoding.UTF8.GetString(ImmagineBinaria); 
      //MessageBox.Show("Immagine in formato binario: " + BinaryImageCast); 

      //inserisco i dati nel database 
      SqlConnection conn = db.apriconnessione(); 

      String query = "Insert into Rapporto(IdCantiere,IdUtenteCreazione,NumeroDocumento,Data,Immagine) values(@IdCantiere,@IdUtenteCreazione,@NumeroDocumento,@Data,@Immagine) "; 

      using (SqlCommand command = new SqlCommand(query, conn)) 
      {      
       command.Parameters.Add("@IdCantiere", SqlDbType.Int).Value = IdCantiere; 
       command.Parameters.Add("@IdUtenteCreazione", SqlDbType.Int).Value = u.IdUtente; 
       command.Parameters.Add("@NumeroDocumento", SqlDbType.Int).Value = int.Parse(textBoxNumeroDocumento.Text); 
       command.Parameters.Add("@Data", SqlDbType.DateTime).Value = dateTimePickerData.Value; 
       command.Parameters.Add("@Immagine", SqlDbType.Binary).Value = ImmagineBinaria; 
       command.ExecuteNonQuery(); 

      } 

      db.chiudiconnessione(); 
      conn.Close(); 

     } 

     else 
     { 
      MessageBox.Show("Devi compilare tutti i campi"); 
     } 



    } 

    catch(Exception ex) 
    { 
     MessageBox.Show("Errore: controlla i formati "+ex); 
    } 

} 

Datenbank SQL Server: enter image description here

Tabellenschema

CREATE TABLE Rapporto(
    IdRapporto int IDENTITY(1,1) PRIMARY KEY, 
    IdCantiere int FOREIGN KEY REFERENCES Cantiere(IdCantiere), 
    IdUtenteCreazione int FOREIGN KEY REFERENCES Utente(IdUtente), 
    NumeroDocumento varchar(5000) default NULL, 
    Data datetime default NULL, 
    Immagine varbinary(MAX) default NULL, 

); 
+0

https://www.akadia.com/services/dotnet_read_write_blob.html – Fildor

+0

Ich denke, „SqlTDbype.Binary“ ist nicht eigentlich das, was Sie denken, es ist. – Fildor

+0

Wie könnte es "binär" sein? es müsste der 'varbinary (max)' oder (obsolete) ein 'image' Datentyp sein. – dlatikay

Antwort

2

Versuchen Sie diese Verwendung conv_photo() das wird Ihnen helfen.

private void buttonCaricaImmagine_Click(object sender, EventArgs e) 
      { 
       OpenFileDialog of = new OpenFileDialog(); 
       //For any other formats 

       of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG"; 
       if (of.ShowDialog() == DialogResult.OK) 
       { 

        pictureBoxRapportino.ImageLocation = of.FileName; 
        imm = pictureBoxRapportino.Image; 
        checkImage = 1; 
       } 
      } 
      //this will convert your picture and save in database 
      void conv_photo() 
      { 
       MemoryStream ms; 
       if (pictureBoxRapportino.Image != null) 
       { 
        ms = new MemoryStream(); 
        pictureBoxRapportino.Image.Save(ms, ImageFormat.Jpeg); 
        byte[] photo_aray = new byte[ms.Length]; 
        ms.Position = 0; 
        ms.Read(photo_aray, 0, photo_aray.Length); 
        command.Parameters.Add("@Immagine", SqlDbType.Binary).Value = photo_aray; 
       } 
      } 



      private void buttonInserimento_Click(object sender, EventArgs e) 
      { 
       try 
       { 
        if (checkImage == 1 && textBoxNumeroDocumento.Text != "") 
        { 
         //controlla dati 
         int NumeroDocumento = int.Parse(textBoxNumeroDocumento.Text); 

         //inserisco i dati nel database 
         SqlConnection conn = db.apriconnessione(); 

         String query = "Insert into Rapporto(IdCantiere,IdUtenteCreazione,NumeroDocumento,Data,Immagine) values(@IdCantiere,@IdUtenteCreazione,@NumeroDocumento,@Data,@Immagine) "; 

         using (SqlCommand command = new SqlCommand(query, conn)) 
         { 
          command.Parameters.Add("@IdCantiere", SqlDbType.Int).Value = IdCantiere; 
          command.Parameters.Add("@IdUtenteCreazione", SqlDbType.Int).Value = u.IdUtente; 
          command.Parameters.Add("@NumeroDocumento", SqlDbType.Int).Value = int.Parse(textBoxNumeroDocumento.Text); 
          command.Parameters.Add("@Data", SqlDbType.DateTime).Value = dateTimePickerData.Value; 
          conv_photo(); 
          command.ExecuteNonQuery(); 

         } 

         db.chiudiconnessione(); 
         conn.Close(); 

        } 

        else 
        { 
         MessageBox.Show("Devi compilare tutti i campi"); 
        } 

       } 

       catch (Exception ex) 
       { 
        MessageBox.Show("Errore: controlla i formati " + ex); 
       } 

      } 
+0

es ist Start danke Jungs – riki

Verwandte Themen