Cara Menyimpan Gambar ke databse SQL Server dengan VB.NET Memory Strream
Minggu, 13 Maret 2016
Edit
Mungkin Anda ingin membuat aplikasi yang dapat menyimpan gambar ke database SQL Server dan mengambilanya kembali dengan Visual Basic .Net Pada artikel yang lalu saya telah membuat contoh kode yang dapat menyimpan gambar ke database sql server melalui File Stream kali ini Anda akan berlatih menyimpan gambar dengan Memory Stream di VB.NET Lihat gambar di bawah ini: |
Berikut adalah kodenya:
Imports System.IO
Imports System.Data.SqlClient
Public Class Form1
Private sConn As String = "Data Source=.;Initial Catalog=dataGambar;Integrated Security=True"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs)Handles Button1.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs)Handles Button2.Click
Dim oConn As New SqlConnection(sConn)
If oConn.State = ConnectionState.Closed Then oConn.Open()
Dim sql As String = "INSERT INTO gambarTbl VALUES(@kode,@gbr)"
Dim oCmd As New SqlCommand(sql, oConn)
With oCmd.Parameters
.AddWithValue("@kode", TextBox1.Text)
.AddWithValue("@gbr", getDataGambar(PictureBox1))
End With
oCmd.ExecuteNonQuery()
MessageBox.Show("kode & gambar telah disimpan", "Simpan", _
MessageBoxButtons.OK)
End Sub
Private Function getDataGambar(ByVal oPic As PictureBox) As Byte()
Dim ms As New MemoryStream()
oPic.Image.Save(ms, oPic.Image.RawFormat)
Dim dataGbr As Byte() = ms.GetBuffer()
Return dataGbr
End Function
Public Sub getData(ByVal sKode As String)
Dim s As String = "select * from gambarTbl where kode='" & sKode & "'"
Dim oconn As New SqlConnection()
oconn.ConnectionString = "Data Source=.;Initial Catalog=dataGambar;Integrated Security=True"
Dim ocmd As SqlCommand = New SqlCommand(s, oconn)
ocmd.CommandType = CommandType.Text
Dim da As SqlDataAdapter = New SqlDataAdapter(ocmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim oTbl As DataTable
oTbl = ds.Tables(0)
If oTbl.Rows.Count > 0 Then
TextBox1.Text = oTbl.Rows(0).Item("kode")
getGbrMem(TextBox1.Text)
End If
End Sub
Private Sub getGbrMem(ByVal sKode As String)
Dim oConn As New SqlConnection(sConn)
If oConn.State = ConnectionState.Closed Then oConn.Open()
Dim oCmd As SqlCommand
oCmd = New SqlCommand("select gbr from gambarTbl where kode='" & _
sKode & "'", oConn)
Dim imageData As Byte() = DirectCast(oCmd.ExecuteScalar(), Byte())
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
PictureBox1.Image = Image.FromStream(ms, True)
End Using
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs)Handles Button3.Click
Form2.Show()
End Sub
End Class
Imports System.Data.SqlClient
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs)Handles MyBase.Load
Dim s As String = "select * from gambarTbl"
Dim oconn As New SqlConnection()
oconn.ConnectionString = "Data Source=.;Initial Catalog=dataGambar;Integrated Security=True"
Dim ocmd As SqlCommand = New SqlCommand(s, oconn)
ocmd.CommandType = CommandType.Text
Dim da As SqlDataAdapter = New SqlDataAdapter(ocmd)
Dim ds As New DataSet()
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal_
e AsSystem.Windows.Forms.DataGridViewCellMouseEventArgs) HandlesDataGridView1.CellMouseDoubleClick
With Form1
.TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value
.getData(.TextBox1.Text)
End With
End Sub
End Class
|
download source code simpan gambar ke database sql server dengan vb.net memory stream |
Buat pembelajar program Visual Basic intermediate ato Advance kode ini tidaklah sulit dimengerti. Namun untuk pemula (newbie) mungkin agak sulit. Jika Anda sangat pemula sekali Anda bisa belajar dari e-book saya yang berjudul: E-BOOK TRIK DASAR EBOOK TUTORIAL TRIK DASAR BELAJAR VISUAL BASIC .NET 2010 Jika Anda ingin lanjut belajar program database sql server dengan visual basic .net bisa dari e-book Saya yang ini E-Book Trik Database Visual Basic 2010 Express |