error-参数无效. [英] error-Parameter is not valid.
问题描述
你好朋友,
我尝试使用以下代码从访问数据库中检索图像:
Hello friends,
I try to retrieve an image from access database using following code:
private void button3_Click(object sender, EventArgs e)
{
OleDbConnection cn = new OleDbConnection(strCn);
cn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT Iname,Oimage FROM [Image]", cn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Image");
int c = ds.Tables["Image"].Rows.Count;
if (c > 0)
{
Byte[] byteOimage = new Byte[0];
byteOimage = System.Text.Encoding.Unicode.GetBytes((ds.Tables["Image"].Rows[c - 1]["Oimage"]).ToString());
MemoryStream strOimage = new MemoryStream(byteOimage);
pictureBox1.Image = Image.FromStream(strOimage);//here i got error message Parameter is not valid.
}
cn.Close();
}
我在代码的最后到第二行中提到我遇到了错误.
那我该怎么解决呢?
请帮帮我.
预先感谢.
I mention in the last to second line of my code that I get the error.
So,how can I solve it?
Please help me.
Thanks in advance.
推荐答案
您正尝试使用零长度的字节数组,其中没有任何内容
You are trying to use a zero length byte array, there is nothing in it
Byte[] byteOimage = new Byte[0];
C#相册查看器 [
顺便说一句,匈牙利表示法(byteOimage,strOimage)几年来都没有使用过.
C# Photo Album Viewer[^]
Look at the Display the image section. Although it is SQL Server, the same techniques should work with Access.
BTW, Hungarian notation (byteOimage, strOimage) hasn''t bee used in years.
我也面临以下相同的问题和解决方案:-
I too faced the same issue and solution given below :-
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rmaheswari\Documents\TestImage.accdb;Persist Security Info=False;");
cn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT Iname,Oimage FROM [Table1]", cn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet(); da.Fill(ds, "Image");
int c = ds.Tables["Image"].Rows.Count; if (c > 0)
{
byte[] rawData = (byte[])ds.Tables["Image"].Rows[c - 1]["Oimage"];
MemoryStream ms = new MemoryStream();
Bitmap bm;
ms.Write(rawData, 78, rawData.Length - 78);
bm = new Bitmap(ms);
pictureBox1.Image = bm;
} cn.Close();}
这是您的问题代码:
This is your problem code:
byteOimage = System.Text.Encoding.Unicode.GetBytes((ds.Tables["Image"].Rows[c - 1]["Oimage"]).ToString());
Access将数据存储为字节数组.但是,然后您将该字节数组转换为字符串.
单步执行代码,并在该行上放置一个断点.然后,添加手表ds.Tables["Image"].Rows[c - 1]["0image"]).ToString()
并查看其内容.
只需摆脱该行上的ToString()即可.您可能还需要像其他答案一样键入强制转换.
Access stores the data as a byte array. But then you take that byte array and convert it to a string.
Step through your code and put a breakpoint in at that line. Then, add a watch for ds.Tables["Image"].Rows[c - 1]["0image"]).ToString()
and see what it says.
Just get rid of the ToString() on that line. You may also have to type cast it as in the other answer.
这篇关于error-参数无效.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!