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);
}
}
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,
);
https://www.akadia.com/services/dotnet_read_write_blob.html – Fildor
Ich denke, „SqlTDbype.Binary“ ist nicht eigentlich das, was Sie denken, es ist. – Fildor
Wie könnte es "binär" sein? es müsste der 'varbinary (max)' oder (obsolete) ein 'image' Datentyp sein. – dlatikay