错误:在基于Windows的应用程序中使用c#从数据库中检索图像时参数无效 [英] error: parameter is not valid while retreiving image from database using c# in windows based application
问题描述
我一直收到此错误:从数据库中检索图像时参数无效。我正在使用以下代码。真的很感激任何帮助。
尝试
{
if (con.State!= ConnectionState.Open)
{
con.Open();
}
SqlCommand cmd1 = new SqlCommand( select * from hbet_info where id =' + textBox1.Text + ',con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
textBox5.Text = dr [ NAME]。ToString();
// textBox1.Text = dr [id]。ToString();
byte [] b = new byte [ 0 ];
b =( Byte [])(dr [ IMG跨度>]);
// ms.Seek(0,SeekOrigin.Begin);
MemoryStream ms = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(ms);
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public class Document1
{
public byte [] DocContent { get ; set ; }
}
public byte [] Attachmnt;
Document1 objDoc = new Document1();
objDoc.DocContent = Attachmnt; //这里你将把你的日期变成byte []格式
File.WriteAllBytes(FileName,objDoc.DocContent);
writeStream = new FileStream(FileName,FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream);
writeBinay.Write(objDoc.DocContent);
writeBinay.Close();
writeStream.Close();
通过这个你的数据将被创建到文件中,现在你可以打开这个文件选择来自此文件的所有数据并显示在文本框中,之后你也可以删除此文件。
SqlCommand cmd1 = new SqlCommand(select * from hbet_info where id =''+ textBox1.Text +'',con);
=> id字段是整数吗?
如果是这样,你不应该把它的值放在引号之间。
而且,构造sql请求是一个非常非常糟糕的习惯字符串连接。
更好:
SqlCommand cmd1 =新的SqlCommand(select * from hbet_info where id = @ id,con);
cmd1.Parameters.AddWithValue(id,int.Parse(textBox1.Text));
insted of this line b =(Byte [])(dr [img]);
使用b =(Byte [])dr [img] ;
I keep getting this error: parameter not valid while retreiving the image from the database.I am using the following code.Any help would really be appreciated.
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id='"+textBox1.Text+"'", con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
textBox5.Text = dr["NAME"].ToString();
//textBox1.Text = dr["id"].ToString();
byte[] b = new byte[0];
b = (Byte[])(dr["img"]);
// ms.Seek(0, SeekOrigin.Begin);
MemoryStream ms = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(ms);
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public class Document1 { public byte[] DocContent { get; set; } }
public byte[] Attachmnt; Document1 objDoc = new Document1(); objDoc.DocContent = Attachmnt;//here u will get ur date into byte[] format
File.WriteAllBytes(FileName, objDoc.DocContent); writeStream = new FileStream(FileName, FileMode.Create); BinaryWriter writeBinay = new BinaryWriter(writeStream); writeBinay.Write(objDoc.DocContent); writeBinay.Close(); writeStream.Close();
By this ur data will be created into file, now u can open this file select all data from this file and display them into textbox,after than u can also delete this file.
SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id=''"+textBox1.Text+"''", con);
=> Is the id field an integer one ?
If so, you should NOT put its value between quotes.
Moreover, it is a very very bad habit to construct sql requests with string concatenations.
Better :
SqlCommand cmd1 = new SqlCommand("select * from hbet_info where id=@id", con); cmd1.Parameters.AddWithValue("id", int.Parse(textBox1.Text));
Hi,
Insted of this line b = (Byte[])(dr["img"]);
useb = (Byte[])dr["img"];
这篇关于错误:在基于Windows的应用程序中使用c#从数据库中检索图像时参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!