2014-01-19 30 views
0

我在我的數據庫中創建了一個表,用於存儲用戶信息和他/她的圖片。我的照片欄有一個image數據類型。無法使用vb.net從我的SQL Server數據庫檢索圖像

我完成我的登記表,並使用此代碼成功添加數據到我的數據庫:

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 

你能幫幫我嗎?

+0

爲什麼在查詢中使用LIKE?哪個尺寸有你的形象? –

+0

我從我選擇的列表視圖中導入我的「user_id」.. hmm。?對不起,你是什麼意思? –

+0

錯誤是否發生在特定的地方,例如屏幕的中間還是某些特定的代碼行?加載第一張圖片或加載幾千張圖片後是否發生? – HABO

回答

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 
0

這裏試試這個...這是你的get ...

改變了...

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 

TO THIS ...

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 

改變你SAVE ROUTINE ...

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先生

+0

在斷點處設置變量「bImg」時設置斷點,按F10一次,然後將鼠標懸停在「 bImg「以確保它不是空的等等。如果不是,那麼你應該很好。 – Codexer

+0

此外,這是你問早前同樣的問題,請不要這樣做... – Codexer

+0

哦對不起... ..我認爲我的問題早些時候有點含糊..所以我開始發表另一個問題.. 我改變了代碼。但它仍然出現內存不足錯誤 –

0

我把它變成這個..

子fillDataFields() 昏暗arrImage爲字節()

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 
相關問題