ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 6167|ตอบกลับ: 3

[VB.NET] การจัดเก็บข้อมูลรูปภาพแบบ BLOB ไว้ในไฟล์ฐานข้อมูล MS Access

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6030




ชนิดข้อมูลแบบ Binary Large OBject (BLOB) หรือวัตถุขนาดใหญ่แบบไบนารี ชนิดข้อมูลนี้เหมาะสำหรับเก็บรูปภาพ, เสียง, กราฟิก หากถามแอดมินว่าเหมาะสมหรือไม่ในการจัดเก็บเข้าสู่ฐานข้อมูล ตอบว่าไม่ดีเลยครับ เพราะจะทำให้ฐานข้อมูลมีขนาดใหญ่โตเทอะทะเอาซ่ะเปล่าๆ แต่ที่แอดมินเอาโค้ดมานำเสนอให้ดู ก็เพื่อให้ไว้ศึกษา หรือประยุกต์ใช้กับงานขนาดเล็กๆก็แล้วกันครับผม โค้ดนี้จะแสดงการโหลดรูปภาพออกมาจากตารางข้อมูล แล้วให้เลือก Browse ไฟล์ภาพใหม่ จากนั้นทำการบันทึกเข้าไปเก็บลงในฐานข้อมูล ส่วนการเพิ่มข้อมูล (Add) แอดมินอยากให้ไปคิดเพิ่มเติมกันเอาเองน่ะข่ะรับกระผม


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / More Info: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Load and save images with BLOB into MS Access.
  10. ' / Microsoft Visual Basic .NET (2010) + MS Access 2007+
  11. ' /
  12. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / --------------------------------------------------------------------------------
  15. Imports System.Data.OleDb
  16. Imports System.IO

  17. Public Class frmStoreImage
  18.     '// Declare public variables only this form.
  19.     Dim Conn As OleDbConnection
  20.     Dim Cmd As OleDbCommand
  21.     Dim DS As DataSet
  22.     Dim DR As OleDbDataReader
  23.     Dim DA As OleDbDataAdapter
  24.     Dim strSQL As String '// Major SQL
  25.     '// Define Path
  26.     Dim strPathData As String = MyPath(Application.StartupPath)
  27.     '// Primary Key
  28.     Dim PK As Long

  29.     Private Sub frmStoreImage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  30.         Call ConnectDataBase()
  31.         Call LoadComboBox()
  32.     End Sub

  33.     ' / Load table detail into ComboBox
  34.     Public Sub LoadComboBox()
  35.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  36.         Try
  37.             strSQL = "SELECT * FROM Car ORDER BY CarID "
  38.             Cmd = New OleDb.OleDbCommand(strSQL, Conn)
  39.             DR = Cmd.ExecuteReader
  40.             Dim DT As DataTable = New DataTable
  41.             DT.Load(DR)
  42.             '/ Primary Key (ValueMember)
  43.             cmbImage.ValueMember = "CarPK"
  44.             '/ Display the name
  45.             cmbImage.DisplayMember = "CarName"
  46.             cmbImage.DataSource = DT
  47.             DR.Close()
  48.             Cmd.Dispose()
  49.             Conn.Close()
  50.         Catch ex As Exception
  51.             MessageBox.Show(ex.Message)
  52.         End Try
  53.     End Sub

  54.     Private Sub cmbImage_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbImage.SelectedIndexChanged
  55.         Call LoadImage()
  56.     End Sub

  57.     Private Sub LoadImage()
  58.         strSQL = _
  59.             " SELECT Car.CarName, Car.PictureDB " & _
  60.             " FROM Car " & _
  61.             " WHERE CarName = " & "'" & cmbImage.Text & "'"
  62.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  63.         Try
  64.             DA = New OleDbDataAdapter(strSQL, Conn)
  65.             DS = New DataSet
  66.             DA.Fill(DS)
  67.             '/ ------------------------------------------------------------------
  68.             With DS.Tables(0)
  69.                 '// Keep Primary Key
  70.                 PK = cmbImage.SelectedValue
  71.                 '// Show Primary Key
  72.                 Label2.Text = "PK = " & PK
  73.                 'MsgBox(PK)
  74.                 '// Load Image
  75.                 Dim imgByte() As Byte
  76.                 If Not IsDBNull(.Rows(0)("PictureDB")) Then
  77.                     imgByte = .Rows(0)("PictureDB")
  78.                     Dim ms As New MemoryStream(imgByte)
  79.                     picData.Image = Image.FromStream(ms)
  80.                     imgByte = Nothing
  81.                 Else
  82.                     imgByte = Nothing
  83.                     picData.Image = Nothing
  84.                 End If

  85.             End With
  86.             DS.Dispose()
  87.             DA.Dispose()
  88.         Catch ex As Exception
  89.             MessageBox.Show(ex.Message)
  90.         End Try
  91.     End Sub

  92.     Private Sub btnLoadImage_Click(sender As System.Object, e As System.EventArgs) Handles btnLoadImage.Click
  93.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()

  94.         ' / Open File Dialog
  95.         With dlgImage
  96.             '.InitialDirectory = strPathImages
  97.             .Title = "Select images"
  98.             .Filter = "Images types (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  99.             .FilterIndex = 1
  100.             .RestoreDirectory = True
  101.         End With
  102.         ' Select OK after Browse ...
  103.         If dlgImage.ShowDialog() = DialogResult.OK Then
  104.             '// New Image
  105.             picData.Image = Image.FromFile(dlgImage.FileName)
  106.         End If

  107.     End Sub

  108.     ' / --------------------------------------------------------------------------------
  109.     ' / Save Images with Primary Key from SelectValue ComboBox.
  110.     Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
  111.         strSQL = _
  112.             " UPDATE Car SET " & _
  113.             " CarPK= @CPK, PictureDB = @PICDB " & _
  114.             " WHERE CarPK= @CPK "
  115.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  116.         Cmd = New OleDb.OleDbCommand
  117.         '// Images from PictureBox
  118.         Dim imgByte As Byte()
  119.         Dim MemStream As New MemoryStream
  120.         '// Have to load image into PictureBox or not?
  121.         If Not IsNothing(Me.picData.Image) Then
  122.             Me.picData.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
  123.             imgByte = MemStream.GetBuffer()
  124.             MemStream.Read(imgByte, 0, MemStream.Length)
  125.         Else
  126.             imgByte = Nothing
  127.         End If
  128.         '// START
  129.         Try
  130.             Cmd.Parameters.AddWithValue("@CPK", PK)
  131.             Cmd.Parameters.AddWithValue("@PICDB", IIf(imgByte IsNot Nothing, imgByte, DBNull.Value))
  132.             '//
  133.             Cmd.Connection = Conn
  134.             Cmd.CommandType = CommandType.Text
  135.             Cmd.CommandText = strSQL
  136.             Cmd.ExecuteNonQuery()
  137.             imgByte = Nothing
  138.             MemStream.Close()
  139.             '// Processing ...
  140.             MessageBox.Show("Records Updated Completed.", "Update Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  141.         Catch ex As Exception
  142.             MessageBox.Show(ex.Message)
  143.         End Try
  144.         Cmd.Parameters.Clear()
  145.         Conn.Close()

  146.     End Sub

  147.     ' / --------------------------------------------------------------------------------
  148.     Public Sub ConnectDataBase()
  149.         Dim strConn As String = _
  150.                 "Provider = Microsoft.ACE.OLEDB.12.0;"
  151.         strConn += _
  152.             "Data Source = " & strPathData & "SampleDB.accdb"

  153.         Try
  154.             '/ Create a connection  
  155.             Conn = New OleDbConnection(strConn)
  156.             '/ Open the connection and execute the select command
  157.             Conn.Open()
  158.         Catch ex As Exception
  159.             MessageBox.Show(ex.Message)
  160.         End Try
  161.     End Sub

  162.     ' / --------------------------------------------------------------------------------
  163.     ' / Get my project path
  164.     ' / AppPath = C:\My Project\bin\debug
  165.     ' / Replace "\bin\debug" with ""
  166.     ' / Return : C:\My Project\
  167.     Function MyPath(AppPath As String) As String
  168.         '/ MessageBox.Show(AppPath);
  169.         AppPath = AppPath.ToLower()
  170.         '/ Return Value
  171.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  172.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  173.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  174.     End Function

  175.     Private Sub frmStoreImage_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  176.         Me.Dispose()
  177.         Application.Exit()
  178.     End Sub

  179. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

33

โพสต์

372

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
372
โพสต์ 2019-10-16 01:12:55 | ดูโพสต์ทั้งหมด

ขอบคุณคับ

4

กระทู้

13

โพสต์

104

เครดิต

Member

Rank: 2

เครดิต
104
โพสต์ 2019-11-22 08:27:02 | ดูโพสต์ทั้งหมด

ขอบคุณครับ อาจารย์

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2019-11-25 16:28:05 | ดูโพสต์ทั้งหมด

ขอบพระคุณครับ อาจารย์
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 16:55 , Processed in 0.171035 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้