如何从数据库到图片框检索多个图像? [英] How to retrive more than one image from database to picturebox?

查看:95
本文介绍了如何从数据库到图片框检索多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我是C#.net的新手,所以我没有足够的知识来解决问题.我试图使用存储过程"ReadImage"检索数据库"Image"的3张图像,就像这样

hi!
I am new on C#.net so i don''t have enough knowledge to solve the problem. I tried to retrive 3 images of database "Image" using stored procedure "ReadImage" which is like this

CREATE proc [dbo].[ReadImage]
@Regid int
as
SELECT [Citizenimage],[Otherimage],[Othersimage] FROM [Image]
WHERE [Reg_id]= @Regid



代码像这样



and code goes like this

private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlConnection con = SqlConnect.DBConnect();
            SqlCommand cmd = new SqlCommand("ReadImage", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Regid", txtRegid.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            da.Fill(dataSet);
            if (dataSet.Tables[0].Rows.Count == 1)
            {
                        Byte[] data = new Byte[0];
                        data = (Byte[])(dataSet.Tables[0].Rows[0]["Citizenimage"]);
                        MemoryStream mem = new MemoryStream(data);
                        PbCitizen.Image = Bitmap.FromStream(mem);

                        Byte[] data1 = new Byte[0];
                        data1 = (Byte[])(dataSet.Tables[0].Rows[0]["Otherimage"]);
                        MemoryStream mem1 = new MemoryStream(data1);
                        pbBack.Image = Bitmap.FromStream(mem1);

                        Byte[] data2 = new Byte[2];
                        data2 = (Byte[])(dataSet.Tables[0].Rows[0]["Othersimage"]);
                        MemoryStream mem2 = new MemoryStream(data2);
                        pbOthers.Image = Image.FromStream(mem2);
                }
                con.Close();
            }



由于没有传递有效参数,因此在"mem1"上发生错误.因此任何人都可以在此主题中为我提供帮助,我们将不胜感激...
谢谢!



There occurs error on "mem1" as no valid parameter is passed.So anyone please help me in this topic you will be highly appreciated...
Thank You!

推荐答案

嘿,做了一些测试(即将从数据库返回的二进制文件写入文件),我做了一些修改以在所有情况下都可以使用(即检查任何图像为空):

Hey did some test(ie writing the binary returned from DB to File), I did some modifications to work it for all scenarios( ie check for any Image is Null):

private static void Search(int Regid)
 {
     SqlConnection con = new SqlConnection();
     con.ConnectionString = "Data Source=RKUTHUPARA;Initial Catalog=Ops;Integrated Security=SSPI;Connect Timeout=60;";
     SqlCommand cmd = new SqlCommand("ReadImage", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@Regid", Regid);
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet dataSet = new DataSet();
     da.Fill(dataSet);
     con.Close();
     if (dataSet.Tables[0].Rows.Count == 1)
     {
         Byte[] data = new Byte[0];
         data = (Byte[])(dataSet.Tables[0].Rows[0]["Citizenimage"] is DBNull? null : dataSet.Tables[0].Rows[0]["Citizenimage"]);
         if (data != null)
         {
             //MemoryStream mem = new MemoryStream(data);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".1.png", data);
         }



         Byte[] data1 = new Byte[0];
         data1 = (Byte[])(dataSet.Tables[0].Rows[0]["Otherimage"] is DBNull ? null : dataSet.Tables[0].Rows[0]["Otherimage"]);
         if (data1 != null)
         {
             //MemoryStream mem1 = new MemoryStream(data1);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".2.png", data1);
         }


         Byte[] data2 = new Byte[2];
         data2 = (Byte[])(dataSet.Tables[0].Rows[0]["Othersimage"] is DBNull ? null : dataSet.Tables[0].Rows[0]["Othersimage"]);
         if (data2 != null)
         {
             //MemoryStream mem2 = new MemoryStream(data2);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".3.png", data2);
         }
     }

 }


因此,您可以立即尝试,也可以在使用它之前请求刷新内存流.
像:


So you can try now, also request you flush memorystream before using it.
like :

if (data != null)
                {
                    MemoryStream mem = new MemoryStream(data);
                    mem.Flush();
                    PbCitizen.Image = Bitmap.FromStream(mem);
                }


这篇关于如何从数据库到图片框检索多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆