本文介绍了如何从数据库中检索img的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好, 当我单击按钮(btnImgFrmDB)时,我开发了一个代码来显示数据库中的图像 它失败了,我从互联网上尝试了很多,但仍然是同样的问题. Error: Parameter is not valid.error showing where i wrote the line in bold and underline 代码:
Hello there I develop a code to show the image from my database when i click button(btnImgFrmDB) it fail and i tried a lot from internet but still the same problem. Error: Parameter is not valid.error showing where i wrote the line in bold and underline Code:
private void btnImgFrmDB_Click_1(object sender, EventArgs e) { if (textImg.Text != "") { string sqlstr = "Select Image from SqlToImage where ImgName=''" + textImg.Text + "'' "; SqlConnection conn = new SqlConnection(constring); conn.Open(); SqlCommand cmd = new SqlCommand(sqlstr, conn); object img = cmd.ExecuteScalar(); byte[] MyImg = (byte[])img; Stream s = null; MemoryStream ms = new MemoryStream(MyImg); ms.Read(MyImg, 0, MyImg.Length); s = ms; pictureBox1.Image = Image.FromStream(ms); //Also tried the below Code and having same problem byte[] MyData = new byte[2048]; MemoryStream ms=null; while (dr.Read()) { ms = new MemoryStream((byte[])dr[0]); } Stream s = ms; s.Read(MyData, 0, 2048); pictureBox1.Image=Image.FromStream(s); myPictureBox.Image = Image.FromStream(ms); ms.Close(); }推荐答案
您尝试以下代码:- You try this code:- private void button3_Click(object sender, EventArgs e) { if (textImg.Text != "") { string sql = "Select Image from SqlToImage where ImgName=''" + textImg.Text + "'' "; SqlConnection connection = new SqlConnection(); connection.ConnectionString = connectionString; connection.Open(); FileStream file; BinaryWriter bw; int bufferSize = 100; byte[] outbyte = new byte[bufferSize]; long retval; long startIndex = 0; savedImageName = textImg.Text; SqlCommand command = new SqlCommand(sql, connection); SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess); while (myReader.Read()) { //string fileName = @"c:\" +textImg.Text+ ".jpeg"; /*file = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(file);*/ file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(file); startIndex = 0; retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize); while (retval == bufferSize) { bw.Write(outbyte); bw.Flush(); startIndex += bufferSize; retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize); } bw.Write(outbyte, 0, (int)retval - 1); bw.Flush(); bw.Close(); file.Close(); } connection.Close(); curImage = Image.FromFile(savedImageName); pictureBox1.Image = curImage; pictureBox1.Invalidate(); connection.Close(); } }
更多推荐
如何从数据库中检索img
发布评论