Tutorials to .com

Tutorials to .com » Dotnet » Database » vb.net access database picture Montaque (original)

vb.net access database picture Montaque (original)

Print View , by: iSee ,Total views: 8 ,Word Count: 427 ,Date: Sat, 13 Jun 2009 Time: 8:47 PM

vb.net access database picture Montaque (original)

MS in Northwnd own database as an example, there is a table which is the Categories, there are four four, one of them is a Picture field type of Image. We must first add a bmp picture to the last line in the Picture, and then read Showing up to the Image control.

Add a SqlDataAdapter1, use the wizard to set up a database connection Northwnd, SQL statements for SELECT [Category ID], [Category Name], Description, Picture FROM Categories. Generate a data set for dataset1. And then add two buttons, respectively, to write image to the database and read the database, there is a Image control to display images.

Add the following code

Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill (DataSet11)
End Sub

'Read the pictures from the database for temporary storage monkey.bmp, and then loaded into the image control inside.
Private Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click
Try
Dim data As Byte () = DataSet11.Tables (0). Rows (7). Item (3)
Dim myfilestream As New System.IO.FileStream (Application.StartupPath & "\ monkey.bmp", IO.FileMode.Create)
myfilestream.Write (data, 0, data.Length)
myfilestream.Close ()
PictureBox1.Image = New Bitmap (Application.StartupPath & "\ monkey.bmp")
Catch
End Try
End Sub


'The C: \ 6.bmp write storage, you can replace your own picture.
Private Sub Button3_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click
Dim myfilestream As New System.IO.FileStream ( "c: \ 6.bmp", IO.FileMode.Open)
Dim data () As Byte
ReDim data (myfilestream.Length - 1)
myfilestream.Read (data, 0, myfilestream.Length)
myfilestream.Close ()
DataSet11.Tables (0). Rows (7). Item (3) = data
SqlDataAdapter1.Update (DataSet11.GetChanges ())
End Sub



.Net Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.