如何从SQL Server数据库中检索图像并在asp .net图像控件上显示? [英] how to retrieve image from sql server database and show on asp .net image control?

查看:75
本文介绍了如何从SQL Server数据库中检索图像并在asp .net图像控件上显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经从前端插入或保存了sql server数据库中的一些图像。现在我想从数据库中检索这些图像并在asp.net图像控件上显示它们。但此时我收到错误文件'C:\Program Files \Common Files \ Mysoftoft Shared\DevServer\10.0\130066191465673972'已经存在。请找到我的问题的解决方案。谢谢........



图像在数据库中插入代码/保存代码:



< pre lang =c#> protected void Button1_Click( object sender,EventArgs e)
{
string image1 = FileUpload1.FileName; / / 我正在从C:\Program Files \Common Files \ microsrosoft shared\DevServer\10.0上传图片
string image2 = FileUpload2.FileName; // i' m从C:\Program Files \Common Files \ microsrosof上传图片t shared\DevServer\10.0
FileStream fs1 = new FileStream(image1,FileMode.Open,FileAccess.Read);
FileStream fs2 = new FileStream(image2,FileMode.Open,FileAccess.Read);
byte [] bimage1 = new byte [fs1.Length];
byte [] bimage2 = new byte [fs2.Length];
fs1.Read(bimage1, 0 ,Convert.ToInt32(fs1.Length));
fs2.Read(bimage2, 0 ,Convert.ToInt32(fs2.Length));
fs1.Close();
fs2.Close();
cn.Open();
SqlParameter sp = new SqlParameter();
sp.SqlDbType = SqlDbType.Image;
sp.ParameterName = @ passport_photo;
sp.ParameterName = @ sign_photo;
sp.Value = bimage1;
sp.Value = bimage2;
SqlCommand cm = new SqlCommand( INSERT INTO ImageCollection值(@img_id, + @ passport_photo, + @ sign_photo),cn);
cm.Parameters.AddWithValue( @ img_id,TextBox1.Text);
cm.Parameters.AddWithValue( @ passport_photo,sp.Value = bimage1);
cm.Parameters.AddWithValue( @ sign_photo,sp.Value = bimage2);
cm.ExecuteNonQuery();
cm.Dispose();
cn.Dispose();
cn.Close();
}







asp.net图像控件上的图像检索代码:



 受保护  void  DropDownList1_TextChanged( object  sender,EventArgs e)
{
cn.Open();
SqlCommand cm = new SqlCommand( select *来自ImageCollection,其中img_id =' + DropDownList1.SelectedItem.ToString()+ ',cn);
SqlDataAdapter da = new SqlDataAdapter(cm);
SqlDataReader dr = cm.ExecuteReader();
尝试
{
如果(dr.Read())
{

string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());
// 位图bmp1 =新位图(image1);
// 位图bmp2 =新位图(image2);
// BinaryReader br = new BinaryReader(bmp1);
FileStream fs1 = new FileStream(image1,FileMode.CreateNew,FileAccess.Write);
FileStream fs2 = new FileStream(image2,FileMode.CreateNew,FileAccess.Write);
byte [] bimage1 =( byte [])dr [ passport_photo];
byte [] bimage2 =( byte [])dr [ sign_photo];
fs1.Write(bimage1, 0 ,bimage1.Length - 1 );
fs2.Write(bimage2, 0 ,bimage2.Length - 1 );
fs1.Flush();
fs2.Flush();
Image1.ImageUrl = 〜/ images / + DropDownList1.SelectedItem.ToString() ;
Image2.ImageUrl = 〜/ images / + DropDownList1.SelectedItem.ToString() ;
}
dr.Close();
cn.Close();
}
catch (例外情况)
{
throw ex;
}

解决方案

SqlDataReader dr = cm.ExecuteReader();

try

{

if(dr.Read())

{



string image1 = Convert.ToString(DateTime.Now.ToFileTime());

string image2 = Convert.ToString(DateTime.Now.ToFileTime());

//位图bmp1 = new Bitmap(image1);

// Bitmap bmp2 = new Bitmap(image2);

// BinaryReader br = new BinaryReader(bmp1);

FileStream fs1 = new FileStream(image1,FileMode.CreateNew,FileAccess.Write);

FileStream fs2 = new FileStream(image2,FileMode.CreateNew,FileAccess.Write);

byte [] bimage1 =(byte [])dr [passport_photo];

byte [] bimage2 =(byte [])dr [sign_photo];

fs1.Write(bi mage1,0,bimage1.Length - 1);

fs2.Write(bimage2,0,bimage2.Length - 1);

fs1.Flush();

fs2.Flush();

Image1.ImageUrl =〜/ images /+ DropDownList1.SelectedItem.ToString();

Image2.ImageUrl =〜/ images /+ DropDownList1.SelectedItem.ToString();

}

dr.Close();

cn.Close ();

}

catch(exception ex)

{

throw ex;

}


执行此类操作的另一种方法是实现处理程序以提供图像。看一下System.Web.IHttpHandler接口。



想法是在你的页面中写一个图像标签:

< asp:image runat =   server imageurl =  〜/ Image.ashx?ID = xxx /> 



然后在你的处理程序实现中,从查询字符串中获取id,检索你正在做的图像,然后直接写入响应流。 Voila。



这种方法的好处是您不需要触摸文件系统,从而消除了安全性和性能问题。请注意,您还需要一个小的web.config配置设置来告诉IIS如何处理对Image.ashx的请求。


Hello, I've inserted or saved some images in sql server database from front end. Now i want to retrieve those images from database and to show them on asp.net image control. But at this point i"m getting error "The file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\130066191465673972' already exists." Please find a solution of my problem. Thanks........

Image insert code/Save Code in database:

protected void Button1_Click(object sender, EventArgs e)
    {
        string image1 = FileUpload1.FileName;//i'm uploading image from "C:\Program Files\Common Files\microsoft shared\DevServer\10.0"
        string image2 = FileUpload2.FileName;//i'm uploading image from "C:\Program Files\Common Files\microsoft shared\DevServer\10.0"
        FileStream fs1 = new FileStream(image1, FileMode.Open, FileAccess.Read);
        FileStream fs2 = new FileStream(image2, FileMode.Open, FileAccess.Read);
        byte[] bimage1 = new byte[fs1.Length];
        byte[] bimage2 = new byte[fs2.Length];
        fs1.Read(bimage1, 0, Convert.ToInt32(fs1.Length));
        fs2.Read(bimage2, 0, Convert.ToInt32(fs2.Length));
        fs1.Close();
        fs2.Close();
        cn.Open();
        SqlParameter sp = new SqlParameter();
        sp.SqlDbType = SqlDbType.Image;
        sp.ParameterName = "@passport_photo";
        sp.ParameterName = "@sign_photo";
        sp.Value = bimage1;
        sp.Value = bimage2;
        SqlCommand cm = new SqlCommand("INSERT INTO ImageCollection values(@img_id," + "@passport_photo,"+"@sign_photo)", cn);
        cm.Parameters.AddWithValue("@img_id",TextBox1.Text);
        cm.Parameters.AddWithValue("@passport_photo",sp.Value=bimage1);
        cm.Parameters.AddWithValue("@sign_photo",sp.Value=bimage2);
        cm.ExecuteNonQuery();
        cm.Dispose();
        cn.Dispose();
        cn.Close();
    }




Code for Image Retrieve on asp.net image control:

protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        cn.Open();
        SqlCommand cm = new SqlCommand("select * from ImageCollection where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);
        SqlDataAdapter da = new SqlDataAdapter(cm);
        SqlDataReader dr = cm.ExecuteReader();
        try
        {
            if (dr.Read())
            {

                string image1 = Convert.ToString(DateTime.Now.ToFileTime());
                string image2 = Convert.ToString(DateTime.Now.ToFileTime());
                //Bitmap bmp1 = new Bitmap(image1);
                //Bitmap bmp2 = new Bitmap(image2);
                //BinaryReader br = new BinaryReader(bmp1);
                FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
                FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
                byte[] bimage1 = (byte[])dr["passport_photo"];
                byte[] bimage2 = (byte[])dr["sign_photo"];
                fs1.Write(bimage1, 0, bimage1.Length - 1);
                fs2.Write(bimage2, 0, bimage2.Length - 1);
                fs1.Flush();
                fs2.Flush();
                Image1.ImageUrl = "~/images/"+DropDownList1.SelectedItem.ToString();
                Image2.ImageUrl = "~/images/"+DropDownList1.SelectedItem.ToString();
            }
            dr.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }

解决方案

SqlDataReader dr = cm.ExecuteReader();
try
{
if (dr.Read())
{

string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());
//Bitmap bmp1 = new Bitmap(image1);
//Bitmap bmp2 = new Bitmap(image2);
//BinaryReader br = new BinaryReader(bmp1);
FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
byte[] bimage1 = (byte[])dr["passport_photo"];
byte[] bimage2 = (byte[])dr["sign_photo"];
fs1.Write(bimage1, 0, bimage1.Length - 1);
fs2.Write(bimage2, 0, bimage2.Length - 1);
fs1.Flush();
fs2.Flush();
Image1.ImageUrl = "~/images/"+DropDownList1.SelectedItem.ToString();
Image2.ImageUrl = "~/images/"+DropDownList1.SelectedItem.ToString();
}
dr.Close();
cn.Close();
}
catch (Exception ex)
{
throw ex;
}


An alternative way of doing this kind of thing would be to implement a handler to serve the image. Have a look at the System.Web.IHttpHandler interface.

The idea would be to write an image tag in your page along the lines of:

<asp:image runat="server" imageurl="~/Image.ashx?ID=xxx" />


Then in your handler implementation, get the id from the querystring, retrieve the image as you're already doing and then write directly to the response stream. Voila.

The benefits of this approach is that you don't need to touch the file system, eliminating security and performance issues. Note that you will also need a small web.config configuration setting to tell IIS how to handle requests to Image.ashx.


这篇关于如何从SQL Server数据库中检索图像并在asp .net图像控件上显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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