如何通过VB.Net使用Asp.Net在SQLServer2008R2中保存和检索图像

如何通过VB.Net使用Asp.Net在SQLServer2008R2中保存和检索图像,asp.net,sql-server,vb.net,Asp.net,Sql Server,Vb.net,我有一个FileUpload fileupload2控件和一个image box控件image1。现在我想保存浏览图像,并在浏览路径时将其显示在图像框中。我正在写下面的代码 代码中有什么问题?当我浏览路径时,如何在图像框中显示它 Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click Connection() Dim cmd As

我有一个FileUpload fileupload2控件和一个image box控件image1。现在我想保存浏览图像,并在浏览路径时将其显示在图像框中。我正在写下面的代码

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

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

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

一种方法是

从数据库中检索imange 将映像保存到服务器中 将映像框传递给映像服务器路径

'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"

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

一种方法是

从数据库中检索imange 将映像保存到服务器中 将映像框传递给映像服务器路径

'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"

您收到了什么错误消息?我看到的一个问题是DBType.Image参数类型。根据MSDN,这是不存在的:在Else之前的MsgBox行中,您还缺少一个结束报价。msgs没有错误。该图像已另存为数据库中的,但问题是,当我将图像检索到crystal report或grid view中时,我看到一个空白图像。您收到了什么错误消息?我看到的一个问题是DBType.image参数类型。根据MSDN,这是不存在的:在Else之前的MsgBox行中,您还缺少一个结束报价。msgs没有错误。该图像被另存为数据库中的图像,但问题是,当我将图像检索到crystal report或grid view中时,我看到一个空白图像。其他方式:在url下的ashx处理程序中检索图像并将其放入输出流。是的,返回字节内容需要一些编程,但对于不完全是初学者的人来说,完全可以阅读文档并在http响应中返回图像字节-1.其他方法:在url下的ashx处理程序中检索图像并将其放入输出流。是的,返回字节内容需要一些编程,但对于不完全是初学者的人来说,完全可以阅读文档并在http响应中返回图像字节-1.