用于从数据库保存和检索图像的代码 [英] code for save and retrive image from database

查看:92
本文介绍了用于从数据库保存和检索图像的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我获取代码.......


如何在数据库中保存图像以及如何在单击图像后如何检索图像.....

plz help me for code .......


how we can save image in data base and how we can retrive that image after clicking image.....

推荐答案

请检查此链接

http://www.shabdar.org/sql-server/105-store-save-images-in-sql-server.html [ http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB- Csharp.aspx [ ^ ]
Please check this links

http://www.shabdar.org/sql-server/105-store-save-images-in-sql-server.html[^]

http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx[^]


'fuPhoto is your FileUpload Control
'Database.conn is your open connection to database
If fuPhoto.HasFile Then
                Dim strFilePath As String = fuPhoto.FileName
                Dim imageSize As Byte() = fuPhoto.FileBytes
                Dim hpfUploadedImage As HttpPostedFile = fuPhoto.PostedFile
                hpfUploadedImage.InputStream.Read(imageSize, 0, CInt(fuPhoto.PostedFile.ContentLength))

                Dim photo As New SqlParameter("@photo", SqlDbType.Image, imageSize.Length)
                photo.Value = imageSize
                Dim cmd As New SqlCommand()
                cmd.CommandText = "update dbmasstudappn set photo=@photo where appno='" & txtApplicationNo.Text & "'"
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add(photo)
                cmd.Connection = Database.conn
                cmd.ExecuteNonQuery()
            End If



要从数据库中检索图像,您必须使用ASP.Net中的处理程序



For retriving image from database you have to use a handler in ASP.Net


进行上传,请尝试此操作

for upload try this

protected void  btnSub_Click(object sender, EventArgs e)
  {
    try
    {
        string filename = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\UploadHRChart\\" + filename));
        string path = "~\\UploadHRChart\\" + filename;
        SqlConnection con = new SqlConnection(connectionString);
        lblinfo.Text = "";
        lblinfo.Text += " Uploaded Successfully ";
        cmd = new SqlCommand("Insert into HRChart(url) values('" + path + "')", con);
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "UploadHRChart");
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}




要检索图像,您可以获取数据列表并将其绑定为





to retrive the images,You can take data list and bind as


string selectSQL = "SELECT url FROM HRChart";
           SqlConnection cnn = new SqlConnection(connectionString);
           SqlDataAdapter adp = new SqlDataAdapter(selectSQL, cnn);
           DataSet ds = new DataSet();
           adp.Fill(ds);
           DataList1.DataSource = ds;
           DataList1.DataBind();


这篇关于用于从数据库保存和检索图像的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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