无法使用vb.net从我的SQL Server数据库中检索图像

时间:2014-01-19 01:07:39

标签: vb.net sql-server-2008

我在数据库中创建了一个表,用于存储用户的信息和他/她的图片。我的图片列中包含image数据类型。

我填写了我的注册表,并使用以下代码成功地将数据添加到我的数据库中:

 Private Sub cmdRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRegister.Click

 comm.CommandText = "insert into Users(user_id, userNo_id, username, password, last_name, middle_name, first_name, course, section, position, address, birthday,picture) values (@field1,@field2,@field3,@field4,@field5, @field6, @field7,@field8, @field9, @field10, @field11,@field12, @photo)"
 comm.Connection = con

 Dim Password As String = String.Empty
 Dim birthday As String
 Dim ms As New MemoryStream

 PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

 Dim data As Byte() = ms.GetBuffer

 Dim p As New SqlParameter("@photo", SqlDbType.Image)
 p.Value = data
 birthday = cmbMonth.Text + " " + cmbDay.Text + ", " + txtYear.Text

 con.Open()
 Password = Encrypt(txtPassword.Text.Trim())
 With comm
     .Parameters.AddWithValue("@field1", txtUserID.Text)
     .Parameters.AddWithValue("@field2", txtUserNo_id.Text)
     .Parameters.AddWithValue("@field3", txtUsername.Text)
     .Parameters.AddWithValue("@field4", Password)
     .Parameters.AddWithValue("@field5", txtLastName.Text)
     .Parameters.AddWithValue("@field6", txtMiddleName.Text)
     .Parameters.AddWithValue("@field7", txtFirstName.Text)
     .Parameters.AddWithValue("@field8", txtCourse.Text)
     .Parameters.AddWithValue("@field9", txtSection.Text)
     .Parameters.AddWithValue("@field10", cmbPosition.Text)
     .Parameters.AddWithValue("@field11", txtAddress.Text)
     .Parameters.AddWithValue("@field12", birthday)
     .Parameters.Add(p)

 End With

 comm.ExecuteNonQuery()
 comm.Dispose()
 MsgBox("Records Successfully Saved")
 clear()
 con.Close()

但是当我试图检索我的数据尤其是图片时......我收到错误“内存不足”

这是我的代码试图检索我的数据和图像..

 Sub fillDataFields()
    Dim mid As String
    Dim last As String
    Dim first As String

    con.Open()

    comm.CommandText = "Select last_name,middle_name,first_name,course, section, address, " & _
                   "birthday, picture from Users where user_id like @uid"
    comm.Connection = con
    comm.Parameters.AddWithValue("@uid", "%" & frmUsers.ListView1.SelectedItems(0).Text & "%")

    dr = comm.ExecuteReader

    While (dr.Read())
        last = (dr("last_name"))
        mid = (dr("middle_name"))
        first = (dr("first_name"))
        txtCourse.Text = (dr("course"))
        txtSection.Text = (dr("section"))
        richtxtAddress.Text = (dr("address"))
        txtBirthday.Text = (dr("birthday"))
        txtName.Text = last + ", " + first + " " + mid

        Dim imageData As Byte() = DirectCast(dr("picture"), Byte())

        If Not imageData Is Nothing Then
            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                ms.Write(imageData, 0, imageData.Length)
                PictureBox1.BackgroundImage = Image.FromStream(ms, True)
            End Using
        End If
    End While

    con.Close()
    dr.Close()
    comm.Dispose()
End Sub
你能帮帮我吗?

3 个答案:

答案 0 :(得分:0)

这里试一试......这是为了你的...

改变这一点......

   Dim imageData As Byte() = DirectCast(dr("picture"), Byte())
    If Not imageData Is Nothing Then
        Using ms As New MemoryStream(imageData, 0, imageData.Length)
            ms.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(ms, True)
        End Using
    End If

到这......

 Dim bImg() As Byte = dr("picture")
 If Not IsNothing(bImg) Then
    Using pStrm As New System.IO.MemoryStream(bImg)
     PictureBox1.BackgroundImage = Image.FromStream(pStrm)
    End Using
 End If

改变你的日常工作......

 Private Sub cmdRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRegister.Click

 comm.CommandText = "insert into Users(user_id, userNo_id, username, password, last_name, middle_name, first_name, course, section, position, address, birthday,picture) values (@field1,@field2,@field3,@field4,@field5, @field6, @field7,@field8, @field9, @field10, @field11,@field12, @photo)"
 comm.Connection = con

 Dim Password As String = String.Empty
 Dim birthday As String
 Dim ms As New MemoryStream

 PictureBox1.Image.Save(ms, ImageFormat.Jpeg)
 Dim data As Byte() = New Byte(ms.Length - 1) {}
 ms.Position = 0 
 ms.Read(data,0,data.Length)

 birthday = cmbMonth.Text + " " + cmbDay.Text + ", " + txtYear.Text

 con.Open()
 Password = Encrypt(txtPassword.Text.Trim())
 With comm
  .Parameters.AddWithValue("@field1", txtUserID.Text)
  .Parameters.AddWithValue("@field2", txtUserNo_id.Text)
  .Parameters.AddWithValue("@field3", txtUsername.Text)
  .Parameters.AddWithValue("@field4", Password)
  .Parameters.AddWithValue("@field5", txtLastName.Text)
  .Parameters.AddWithValue("@field6", txtMiddleName.Text)
  .Parameters.AddWithValue("@field7", txtFirstName.Text)
  .Parameters.AddWithValue("@field8", txtCourse.Text)
  .Parameters.AddWithValue("@field9", txtSection.Text)
  .Parameters.AddWithValue("@field10", cmbPosition.Text)
  .Parameters.AddWithValue("@field11", txtAddress.Text)
  .Parameters.AddWithValue("@field12", birthday)
  .Parameters.AddWithValue("@photo", data)
End With

comm.ExecuteNonQuery()
comm.Dispose()

MsgBox("Records Successfully Saved")
clear()
con.Close()

干杯! Codexer先生

答案 1 :(得分:0)

我把它改成了..

Sub fillDataFields()         Dim arrImage As Byte()

    con.Open()
    comm.CommandText = "Select last_name + ', ' + first_name + ' ' + middle_name as name,course, section, address, " & _
                   "birthday, picture from Users where user_id like @uid"
    comm.Connection = con
    comm.Parameters.AddWithValue("@uid", "%" & frmUsers.ListView1.SelectedItems(0).Text & "%")
    dr = comm.ExecuteReader
    While (dr.Read())
        arrImage = dr.Item("picture")
        Dim mstream As New System.IO.MemoryStream(arrImage)
        txtCourse.Text = (dr("course"))
        txtSection.Text = (dr("section"))
        richtxtAddress.Text = (dr("address"))
        txtBirthday.Text = (dr("birthday"))
        txtName.Text = (dr("name"))
        PictureBox1.Image = Image.FromStream(mstream)
    End While
    con.Close()
    dr.Close()
    comm.Dispose()
End Sub

答案 2 :(得分:0)

我将保存例程改为此...

        Dim filesize As UInt32
        If PictureBox1.Image Is Nothing Then
        MsgBox("Please upload some photo to continue", MsgBoxStyle.Information,      "Attention")
    Else
        Dim mstream As New System.IO.MemoryStream()
        PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim arrImage() As Byte = mstream.GetBuffer()
        filesize = mstream.Length
        mstream.Close()

        Try
            con.Open()
            comm.CommandText = "insert into Users(user_id, userNo_id, username, password, last_name, middle_name, first_name, course, section, position, address, birthday,picture) values (@field1,@field2,@field3,@field4,@field5, @field6, @field7,@field8, @field9, @field10, @field11,@field12, @photo)"
            comm.Connection = con
            Dim Password As String = String.Empty
            Dim birthday As String
            birthday = cmbMonth.Text + " " + cmbDay.Text + ", " + txtYear.Text
            Password = Encrypt(txtPassword.Text.Trim())

            With comm
                .Parameters.AddWithValue("@field1", txtUserID.Text)
                .Parameters.AddWithValue("@field2", txtUserNo_id.Text)
                .Parameters.AddWithValue("@field3", txtUsername.Text)
                .Parameters.AddWithValue("@field4", Password)
                .Parameters.AddWithValue("@field5", txtLastName.Text)
                .Parameters.AddWithValue("@field6", txtMiddleName.Text)
                .Parameters.AddWithValue("@field7", txtFirstName.Text)
                .Parameters.AddWithValue("@field8", txtCourse.Text)
                .Parameters.AddWithValue("@field9", txtSection.Text)
                .Parameters.AddWithValue("@field10", cmbPosition.Text)
                .Parameters.AddWithValue("@field11", txtAddress.Text)
                .Parameters.AddWithValue("@field12", birthday)
                .Parameters.AddWithValue("@photo", arrImage)
            End With
            comm.ExecuteNonQuery()
            comm.Dispose()
            MsgBox("Records Successfully Saved")
            clear()
            con.Close()

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End If


End Sub