sql server - Save Binary image into SqlServer 2014 from C# -


i have problem code c# below, should save binary image in sql server database 2014, did function convert image binary, image select button, problem when save database 0x00 in field immagine, how can fix type of error? immagine field format binary

query:

private void buttoncaricaimmagine_click(object sender, eventargs e) {                 openfiledialog of = new openfiledialog();     //for 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 dati nel database             sqlconnection conn = db.apriconnessione();              string query = "insert 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 campi");         }        }      catch(exception ex)     {         messagebox.show("errore: controlla formati "+ex);     }  } 

database sql server: enter image description here

table schema

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

try use conv_photo() you.

  private void buttoncaricaimmagine_click(object sender, eventargs e)             {                 openfiledialog of = new openfiledialog();                 //for 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 convert picture , 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 dati nel database                         sqlconnection conn = db.apriconnessione();                          string query = "insert 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 campi");                     }                  }                  catch (exception ex)                 {                     messagebox.show("errore: controlla formati " + ex);                 }              } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -