2014-09-20 65 views
-1
Private Sub UpdatePicture() 

    Dim str As String 
    str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UsersDB.accdb" 
    cn = New OleDbConnection(str) 
    cn.Open() 

    Dim ms As New MemoryStream() 
    Dim arrimage() As Byte 
    If (PictureBox1.Image IsNot Nothing) Then 
     PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat) 
     arrimage = ms.GetBuffer 
     ms.Close() 
    End If 

    With cmd 
     .Connection = cn 
     .CommandText = "UPDATE Users set Picture = @img where StudentNumber " & TextBox1.Text & "" 
     .Parameters.Add("@img", OleDbType.Binary).Value = IIf(PictureBox1.Image IsNot Nothing, arrimage, DBNull.Value) 
     'con.Open() 
     i = .ExecuteNonQuery() 
     .Dispose() 
     cn.Close() 
     If (i > 0) Then 
      MsgBox("Save Successs!") 
     End If 
    End With 
    con.Close() 

End Sub 

有人能給我的代碼顯示/檢索或獲取圖像從我的PictureBox1.Image從Access數據庫根據此代碼?如何顯示/檢索或從Access數據庫獲取圖像到PictureBox?

+0

請張貼代碼顯示您如何嘗試解決您的問題。 StackOverflow不是給你的代碼網站。請提出問題並在提問時遵循以下建議:http://stackoverflow.com/help/how-to-ask和http://stackoverflow.com/help/mcve – jordanhill123 2014-09-20 02:12:39

+0

您可以使用例如' PictureBox1.Image.Save()''您可以從中獲取要存儲的字節。要檢索,使用'PictureBox1.Image = Image.FromXXX()'方法將二進制數據讀入圖像。 – Basic 2014-09-20 02:16:24

+0

[從訪問數據庫檢索圖片]的可能的重複(http://stackoverflow.com/questions/20890646/retrieve-picture-from-access-database) – Plutonix 2014-09-20 11:26:15

回答

0

我剛figgured出來,這是溶液


公共類形式

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\UsersDB.accdb") 
Dim cmd As New OleDbCommand("", con) 
Dim Reader As OleDb.OleDbDataReader 
Dim cn As New OleDbConnection 
Dim i As Integer 

公用Sub的GetData()

con.Open() 
    Dim dt As New DataTable("Users") 
    Dim rs As New OleDb.OleDbDataAdapter("Select * from Users where StudentNumber='" & TextBox1.Text & "' ", con) 
    rs.Fill(dt) 
    DataGridView1.DataSource = dt 
    DataGridView1.Refresh() 
    Label1.Text = dt.Rows.Count 
    rs.Dispose() 
    con.Close() 

    If Val(Label1.Text) = 1 Then 
     Dim i As Integer 
     i = DataGridView1.CurrentRow.Index 
     'Image 
     Dim bytes As [Byte]() = (DataGridView1.Item(6, i).Value) 
     Dim ms As New MemoryStream(bytes) 
     PictureBox1.Image = Image.FromStream(ms) 

    End If 

結束子

完類


相關問題