Jumat, 23 Maret 2012

Menampilkan File PDF dari Mysql dengan VB.net

Masih dioprek belum ketemu ni..... kalau ada yang bisa tolong di posting ya...berhadiah lho....
ini yg lagi saya oprek...

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Tampil
'Inherits System.Windows.Forms.Form

Private conn As MySqlConnection
Private adapter As MySqlDataAdapter
Private ds As DataSet

Private Sub Tampil_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sql As String = "SELECT * FROM data_blob"
Dim ConnStr As String = "server=localhost;uid=root;database=datablob;"
Try
conn = New MySqlConnection(ConnStr)
conn.Open()
ds = New DataSet
adapter = New MySqlDataAdapter(sql, conn)
adapter.Fill(ds, "data_blop")
BindListControls()

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
End Try
End Sub

Private Sub BindListControls()
cbxkode.DataSource = ds.Tables(0)
cbxkode.DisplayMember = "kode_file"
cbxdes.DataSource = ds.Tables(0)
cbxdes.DisplayMember = "deskripsi"
cbxnama.DataSource = ds.Tables(0)
cbxnama.DisplayMember = "nama_file"
cbxsize.DataSource = ds.Tables(0)
cbxsize.DisplayMember = "ukuran"
End Sub

Private Sub btn_lihat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_lihat.Click
Dim CurrFile As Image
Dim fs As FileStream = Nothing

'Deklarasi variabel untuk temporary file
Dim temp As String = "D:\\" + cbxnama.Text

'SQL dengan kondisi kode_file
Dim sql As String = "SELECT * FROM data_blob WHERE kode_file='" & cbxkode.Text & "'"
Try
adapter = New MySqlDataAdapter(sql, conn)
ds = New DataSet
adapter.Fill(ds, "data_blob")
Dim myRow As DataRow

'Menetapkan Index data
myRow = ds.Tables("data_blob").Rows(0)

Dim DataFL() As Byte
DataFL = myRow("file")
Dim Counter As Long

'Mendapatkan jumlah total Elemen array
Counter = UBound(DataFL)
fs = New FileStream(temp, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(DataFL, 0, Counter)
fs.Close()
adapter.Dispose()

Catch ex As Exception
MsgBox("Error : " + ex.Message)
Finally
'Menampilkan File
PDF_Tampil.OcxState = Nothing
'CurrFile = au
PDF_Tampil.OcxState = PDF_Tampil.OcxState
End Try
End Sub

Private Sub bt_keluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_keluar.Click
End
End Sub
End Class

Simpan File PDF ke MySql Database

Tutorial Langka....
sudah kemana2 cari cara untuk memasukkan file pdf ke database mysql gak pernak ketemu atau belum ketemu akhirnya coba2 sendiri dengan mengubah-ubah script orang lain yang tadinya diperuntukkan memasukkan gambar kedalam database mysql, lalu diubah agar bisa memasukkan file pdf, akhirnya bisa juga meskipun belum sempurna.

Berikut scriptnya barangkali ada yg perlu untuk membuat semacam liberary file pdf....

Imports System.IO
Imports MySql.Data.MySqlClient
Imports System.Object
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private adapter As MySqlDataAdapter
Private currFile As String = Nothing
Dim table As DataTable
Dim mysqlCmd As MySqlCommand
Dim CommandText As MySqlCommand
Dim ds As DataSet
Dim fs As FileStream
Sub Tabel()
With LV1
.Columns.Add("KODE", 0, HorizontalAlignment.Center)
.Columns.Add("KATEGORI", 100, HorizontalAlignment.Center)
.Columns.Add("NOMOR", 75, HorizontalAlignment.Center)
.Columns.Add("TAHUN", 75, HorizontalAlignment.Center)
.Columns.Add("TENTANG", 500, HorizontalAlignment.Left)
.GridLines = True
.FullRowSelect = True
.View = View.Details
End With
End Sub
Sub TampilLV()
Dim DTLV As MySqlDataReader
Try
Conn.Open()
Dim CC As New MySqlCommand("select * from MASTER ", Conn)
DTLV = CC.ExecuteReader()
Dim i As Integer = 0
LV1.Items.Clear()
While DTLV.Read

LV1.Items.Add(DTLV("KOde"))
LV1.Items(i).SubItems.Add(DTLV("Kategori"))
LV1.Items(i).SubItems.Add(DTLV("Nomor"))
LV1.Items(i).SubItems.Add(DTLV("Tahun"))
LV1.Items(i).SubItems.Add(DTLV("Tentang"))
i = i + 1
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close() ' must close after execute
End Try

End Sub
Private Sub FrmKosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
ComboBox1.Text = ""
WebBrowser1.Navigate("")
End Sub
Private Sub FrmMati()
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
ComboBox1.Enabled = False
End Sub
Private Sub FrmHidup()
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
ComboBox1.Enabled = True
End Sub
Private Sub FrmAwal()
FrmKosong()
FrmMati()
Button1.Text = "Baru"
Button2.Text = "Edit"
Button3.Text = "Hapus"
Button4.Text = "Tutup"
Button5.Text = "Open Pdf File"
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tabel()
TampilLV()
Conn.Open()
Dim query As String = "SELECT * FROM KTGR"
Dim DA = New MySqlDataAdapter(query, Conn)
Dim DS = New DataSet
DA.Fill(DS, "KTGR")
ComboBox1.Text = ""
With ComboBox1
.DataSource = DS.Tables("KTGR")
.DisplayMember = "Kategori"
.ValueMember = "Nomor"
.SelectedIndex = 0
.Text = ""
End With
Conn.Close()
FrmAwal()
TextBox2.MaxLength = 4
TextBox1.MaxLength = 4
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ukuran As Integer
Dim fs As FileStream

If Button1.Text = "Baru" Then
Button1.Text = "Simpan"
Button2.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
FrmHidup()
TextBox1.Focus()
FrmKosong()
Else


If ComboBox1.Text = "" Or TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
MsgBox("Data Belum Lengkap...!", 48, "Error")
Else

Try
fs = New FileStream(currFile, FileMode.Open, FileAccess.Read)
ukuran = fs.Length
Dim rawData() As Byte = New Byte(ukuran) {}
fs.Read(rawData, 0, ukuran)
fs.Close()

Dim sql As String = "SELECT * FROM MASTER"
conn = New MySqlConnection(ConnectionString)
conn.Open()

adapter = New MySqlDataAdapter(sql, Conn)
Dim CmdBuilder As MySqlCommandBuilder = New MySqlCommandBuilder(adapter)
Dim ds As DataSet = New DataSet("MASTER")
adapter.Fill(ds, "MASTER")
Dim row As DataRow = ds.Tables("MASTER").NewRow()
row("Kode") = ComboBox1.Text + TextBox1.Text
row("kategori") = ComboBox1.Text
row("nomor") = TextBox1.Text
row("tahun") = TextBox2.Text
row("tentang") = TextBox3.Text
row("na_file") = Path.GetFileName(TPath.Text)
row("ukuran") = ukuran
row("file") = rawData
ds.Tables("MASTER").Rows.Add(row)
adapter.Update(ds, "MASTER")
MsgBox("FIle Sudah Tersimpan", MsgBoxStyle.Information, "OK")
conn.Close()
FrmAwal()
TampilLV()
Catch ex As Exception
MsgBox("Gagal Menyimpan Data Silahkan Mengulang...! " + ex.Message)
End Try
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "Edit" Then
Button2.Text = "Simpan"
Button1.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
FrmHidup()
TextBox1.Focus()
Else
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Maaf Data Masih Kurang", 48, "Error")
Else
Conn.Open()
Dim SQLUbah As String
SQLUbah = "Update Master Set Kategori='" & ComboBox1.Text & "',Nomor='" & TextBox1.Text & "',Tahun='" _
& TextBox2.Text & "',Tentang='" & TextBox3.Text & "'Where Kode='" & LBKODE.Text & "'"
Cmd = New MySqlCommand(SQLUbah, Conn)
Cmd.ExecuteNonQuery()
MsgBox("Data Telah Di Ubah", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Sukses")
FrmAwal()
Conn.Close()
TampilLV()
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = "" Then
MsgBox("Maaf Tidak ada data yang akan dihapus", 48, "Perhatian")
Else
Dim Pesan As String
Pesan = MsgBox("Yakin akan dihapus", MsgBoxStyle.YesNo, "Konfirmasi")
If Pesan = vbYes Then
Conn.Open()
Dim SQLHapus As String
SQLHapus = "Delete From Master Where Kode='" & LBKODE.Text & "'"
Cmd = New MySqlCommand(SQLHapus, Conn)
Cmd.ExecuteNonQuery()
Conn.Close()
FrmAwal()
TampilLV()
End If
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Select Case Button4.Text
Case "Tutup"
Close()
Case "Batal"
FrmAwal()
End Select
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
OpenDlg.Title = "Mencari File Data"
If (OpenDlg.ShowDialog() = DialogResult.OK) Then
currFile = OpenDlg.FileName
TPath.Text = currFile
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
End If
End Sub
Private Sub Lv1_DoubleClick1(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV1.DoubleClick
With Me.LV1
LBKODE.Text = .SelectedItems(0).SubItems(0).Text
ComboBox1.Text = .SelectedItems(0).SubItems(1).Text
TextBox1.Text = .SelectedItems(0).SubItems(2).Text
TextBox2.Text = .SelectedItems(0).SubItems(3).Text
TextBox3.Text = .SelectedItems(0).SubItems(4).Text
End With
TampilPdf()
Button4.Text = "Batal"
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then
e.Handled() = True

End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then
e.Handled() = True
End If
End Sub
Sub TampilPdf()
Dim fs As FileStream = Nothing

Dim temp As String = "D:\DataSiskumTemp\" + LBKODE.Text

Dim sql As String = "SELECT * FROM Master WHERE Kode='" & LBKODE.Text & "'"
Try
adapter = New MySqlDataAdapter(sql, Conn)
ds = New DataSet
adapter.Fill(ds, "Master")
Dim myRow As DataRow

myRow = ds.Tables("Master").Rows(0)

Dim DataPDF() As Byte
DataPDF = myRow("file")
Dim Counter As Long

Counter = UBound(DataPDF)
fs = New FileStream(temp, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(DataPDF, 0, Counter)
fs.Close()
adapter.Dispose()

Catch ex As Exception
MsgBox("Error : " + ex.Message)
Finally
WebBrowser1.Refresh()
WebBrowser1.Navigate(temp)
End Try
End Sub
Private Sub TPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TPath.TextChanged
If Not (TPath.Text = String.Empty) Then
WebBrowser1.Navigate(TPath.Text)
End If
End Sub

End Class