[英]Problem retrieving the image after updating the Access database (System.ArgumentException: 'Parameter is not valid.')
First of all I am sorry for my language grammar because my first language is Persian (Iran).首先,我为我的语言语法感到抱歉,因为我的第一语言是波斯语(伊朗)。
I use the following code to insert the image and retrieve it from the Access database, and I have no problem retrieving it after inserting the image.我使用以下代码插入图像并从Access数据库中检索它,插入图像后检索它没有问题。
BitmapImage BM;
private void UploadButton_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
System.Windows.Forms.OpenFileDialog OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
OpenFileDialog.AutoUpgradeEnabled = false;
if (App.EnumLanguage.Equals(AllLanguage.English))
{
OpenFileDialog.Title = "Selecting Image";
}
else
{
OpenFileDialog.Title = "انتخاب تصویر";
}
OpenFileDialog.Filter = "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp|GIF(*.gif)|*.gif|PNG(*.png)|*.png|All Files|*.*";
if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var IMG = System.Drawing.Image.FromFile(OpenFileDialog.FileName);
BM = new BitmapImage(new Uri(OpenFileDialog.FileName));
BitmapImage BitMapImage = new BitmapImage();
BitMapImage.BeginInit();
System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();
IMG.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);
BitMapImage.StreamSource = MemoryStream;
BitMapImage.EndInit();
BookImage.Source = BitMapImage;
OpenFileDialog.Dispose();
}
}
private static byte[] ImageToBytes(BitmapImage image)
{
byte[] Data;
JpegBitmapEncoder JpegEncoder = new JpegBitmapEncoder();
JpegEncoder.Frames.Add(BitmapFrame.Create(image));
using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
{
JpegEncoder.Save(MS);
Data = MS.ToArray();
}
return Data;
}
private BitmapImage GetImageFromBytes(byte[] bytes)
{
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
Stream.Write(bytes, 0, bytes.Length);
Stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(Stream);
BitmapImage bitImage = new BitmapImage();
bitImage.BeginInit();
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
MS.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.StreamSource = MS;
bitImage.EndInit();
return bitImage;
}
private void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
OleDbConnect.Open();
if (BM != null)
{
byte[] Image_Bytes = ImageToBytes(BM);
OleDbParameter Parameter = new OleDbParameter();
Parameter.OleDbType = OleDbType.Binary;
Parameter.ParameterName = "Image";
Parameter.Value = Image_Bytes;
OleDbCommand OleDbCommand_Insert = new OleDbCommand("Insert Into [BookTable](BookName,Publisher,Category,IDNumber,Status,HistoryTaken,RecipientName,ReturnDate,BookImage)values('" + BookName_TextBox.Text + "','" + Publisher_TextBox.Text + "','" + Category_ComboBox.Text + "','" + IDNumber_TextBox.Text + "','" + Status_ComboBox.Text + "','" + HistoryTaken_TextBox.Text + "','" + RecipientName_TextBox.Text + "','" + ReturnDate_TextBox.Text + "',@Image)", OleDbConnect);
OleDbCommand_Insert.Parameters.Add(Parameter);
OleDbCommand_Insert.ExecuteScalar();
}
OleDbConnect.Close();
}
\\\ retrieving information from Access Database
OleDbCommand OleDCmd = new OleDbCommand("Select * From BookTable Where IDNumber='" + Search_ComboBox.Text.Trim() + "'", OleDbConnect);
OleDCmd.CommandType = System.Data.CommandType.Text;
OleDbConnect.Open();
OleDbDataReader DataReader = OleDCmd.ExecuteReader();
while (DataReader.Read())
{
BookName_TextBox.Text = DataReader[0].ToString();
Publisher_TextBox.Text = DataReader[1].ToString();
Category_ComboBox.Text = DataReader[2].ToString();
IDNumber_TextBox.Text = DataReader[3].ToString();
Status_ComboBox.Text = DataReader[4].ToString();
HistoryTaken_TextBox.Text = DataReader[5].ToString();
RecipientName_TextBox.Text = DataReader[6].ToString();
ReturnDate_TextBox.Text = DataReader[7].ToString();
BitmapImage BMP = GetImageFromBytes((byte[])DataReader[8]);
BookImage.Source = BMP;
}
But when I use the following commands to update the image and other information, this error is displayed when I want to retrieve the information.但是当我使用以下命令更新图像和其他信息时,当我想检索信息时会显示此错误。
byte[] Image_Bytes = ImageToBytes(BM);
OleDbParameter Parameter = new OleDbParameter();
Parameter.OleDbType = OleDbType.Binary;
Parameter.ParameterName = "Image";
Parameter.Value = Image_Bytes;
OleDbCommand OleDbCommand_Update = new OleDbCommand("Update [BookTable] Set BookName='"+BookName_TextBox.Text.Trim()+"',Publisher='"+Publisher_TextBox.Text.Trim()+"',Category='"+Category_ComboBox.Text.Trim()+"',Status='"+Status_ComboBox.Text.Trim()+"',HistoryTaken='"+HistoryTaken_TextBox.Text.Trim()+"',RecipientName='"+RecipientName_TextBox.Text.Trim()+"',ReturnDate='"+ReturnDate_TextBox.Text.Trim() +"',BookImage='"+ "@Image" + "'Where IDNumber='" + IDNumber_TextBox.Text.Trim()+ "'", OleDbConnect);
OleDbCommand_Update.Parameters.Add(Parameter);
OleDbCommand_Update.ExecuteScalar();
System.ArgumentException: 'Parameter is not valid.' System.ArgumentException:“参数无效。” Line 6 gives an error
第 6 行给出了一个错误
private BitmapImage GetImageFromBytes(byte[] bytes)
{
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
Stream.Write(bytes, 0, bytes.Length);
Stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(Stream);\\System.ArgumentException: 'Parameter is not valid.'
BitmapImage bitImage = new BitmapImage();
bitImage.BeginInit();
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
MS.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.StreamSource = MS;
bitImage.EndInit();
return bitImage;
}
In short:in insert command i have no problem to retrieve image but in update command i have problem to retrieve image.简而言之:在插入命令中检索图像没有问题,但在更新命令中检索图像有问题。
In my opinion, the Update command does not save the image properly in the Access database.在我看来, Update命令并没有将图像正确地保存在 Access 数据库中。
Thanks谢谢
Hooray, hooray I found the solution (I found it myself).万岁,万岁,我找到了解决方案(我自己找到了)。 if IDNumber data type as be number data type in Access Database, this code works 100%.
如果 IDNumber 数据类型是 Access 数据库中的数字数据类型,则此代码 100% 有效。
Just Replace this code...只需替换此代码...
OleDbCommand OleDbCommand_Update = new OleDbCommand("Update [BookTable] Set BookName='"+BookName_TextBox.Text.Trim()+"',Publisher='"+Publisher_TextBox.Text.Trim()+"',Category='"+Category_ComboBox.Text.Trim()+"',Status='"+Status_ComboBox.Text.Trim()+"',HistoryTaken='"+HistoryTaken_TextBox.Text.Trim()+"',RecipientName='"+RecipientName_TextBox.Text.Trim()+"',ReturnDate='"+ReturnDate_TextBox.Text.Trim() +"',BookImage='"+ "@Image" + "'Where IDNumber='" + IDNumber_TextBox.Text.Trim()+ "'", OleDbConnect);
With the following code (this is how the UPDATE Query command should be written)使用以下代码(这是 UPDATE Query 命令的编写方式)
OleDbCommand OleDbCommand_Update = new OleDbCommand("Update [BookTable] Set BookImage=Image,BookName='" +BookWindow.BookName_TextBox.Text.Trim() + "',Publisher='" + BookWindow.Publisher_TextBox.Text.Trim() + "',Category='" + BookWindow.Category_ComboBox.Text.Trim() + "',Status='" + BookWindow.Status_ComboBox.Text.Trim() + "',HistoryTaken='" + BookWindow.HistoryTaken_TextBox.Text.Trim() + "',RecipientName='" + BookWindow.RecipientName_TextBox.Text.Trim() + "',ReturnDate='" + BookWindow.ReturnDate_TextBox.Text.Trim() + "'Where IDNumber=" + BookWindow.IDNumber_TextBox.Text.Trim(), OleDbConnect);
I tested a lot until I finally got the result.我测试了很多,直到最终得到结果。 Thanks a lot for your help.
非常感谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.