如何通过 VB.Net 使用 Asp.Net 在 SQL Server 2008R2 中保存和检索图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13488239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:14:21  来源:igfitidea点击:

How to save and retrieve image in SQL Server 2008R2 using Asp.Net via VB.Net

asp.netsql-servervb.net

提问by farzana

I have a FileUpload fileupload2control and an image box control image1. Now I want to save the browsing image and also show it on image box when I browsing the path. I am writing the code like below.

我有一个 FileUploadfileupload2控件和一个图像框控件image1。现在我想保存浏览图像,并在浏览路径时将其显示在图像框中。我正在编写如下代码。

What's the problem in that code and how I show it on image box when I browsing the path?

该代码有什么问题,以及在浏览路径时如何在图像框中显示它?

Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click
    Connection()

    Dim cmd As New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "INSERT INTO Personal_info (image_id,pic)VALUES (@image_id,@pic)"

    cmd.Parameters.Add("@image_id", SqlDbType.VarChar, 200)
    cmd.Parameters.Add("@pic", SqlDbType.Image, 200)

    cmd.Parameters("@image_id").Value = txtid.Text
    cmd.Parameters("@pic").Value = FileUpload2.FileBytes

    Dim i = -1

    i = cmd.ExecuteNonQuery

    If i = -1 Then
        lblMessage.ForeColor = Drawing.Color.Red
        lblMessage.Text = "Fail to Save"
        'MsgBox("Fail", MsgBoxStyle.OkOnly, "Error")
    Else
        lblMessage.ForeColor = Drawing.Color.Green
        lblMessage.Text = "Success to Save"
    End If
End Sub

回答by vasa

to show the imange in the image box you need to provide the imange path or url. you cannot read it directly from the database.

要在图像框中显示图像,您需要提供图像路径或 url。你不能直接从数据库中读取它。

one way to do it is to

一种方法是

  1. retrieve the imange from the database
  2. save the image in the server
  3. pass the image box the image server path

    '1. retrieve the imange from the database
    Dim cmd As New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "SELECT pic FROM Personal_info  WHERE image_id=@image_id;"
    
    cmd.Parameters.Add("@image_id", image_id)
    
    Dim rd As SqlDataReader = cmd.ExecuteReader
    Dim MyData() As Byte
    If rd.Read Then
        If rd("pic").ToString <> Nothing Then
            MyData = rd("pic")
        End If
    
    End If
    
    '2. save the image in the server
    Dim oFileStream As New System.IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & "image\image.jpg", System.IO.FileMode.Create, IO.FileAccess.ReadWrite)
    oFileStream.Write(MyData, 0, MyData.Length)
    oFileStream.Close()
    
    '3. pass the image box the image server path
    image1.ImageUrl = "~/image/image.jpg"
    
  1. 从数据库中检索图像
  2. 将图像保存在服务器中
  3. 将图像框传递给图像服务器路径

    '1. retrieve the imange from the database
    Dim cmd As New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "SELECT pic FROM Personal_info  WHERE image_id=@image_id;"
    
    cmd.Parameters.Add("@image_id", image_id)
    
    Dim rd As SqlDataReader = cmd.ExecuteReader
    Dim MyData() As Byte
    If rd.Read Then
        If rd("pic").ToString <> Nothing Then
            MyData = rd("pic")
        End If
    
    End If
    
    '2. save the image in the server
    Dim oFileStream As New System.IO.FileStream(System.AppDomain.CurrentDomain.BaseDirectory & "image\image.jpg", System.IO.FileMode.Create, IO.FileAccess.ReadWrite)
    oFileStream.Write(MyData, 0, MyData.Length)
    oFileStream.Close()
    
    '3. pass the image box the image server path
    image1.ImageUrl = "~/image/image.jpg"