Menu Horisontal

Tampilkan postingan dengan label Database. Tampilkan semua postingan
Tampilkan postingan dengan label Database. Tampilkan semua postingan

Kamis, 07 Juni 2012

Save dan Load File Gambar dengan SQL Server dan VB.Net 2008


Postingan kali ini tentang gambar, yaitu cara save image ataupun load image di VB.Net 2008. Gambar dalam hal ini akan ditampilkan melalui PictureBox.

Pertama-tama, di SQL Server siapkan sebuah table bernama tbl_img (nama database = testing).

Data di tabel hanya contoh, boleh diabaikan. Tabel tbl_img ini untuk menyimpan nama file dengan Path gambar yang kita akan gunakan.

Langsung saja buat sebuah project baru di VB.Net dan rancang form seperti berikut ini :


Catatan : Disamping objek yang terlihat diatas, saya juga menggunakan OpenFileDialog.

Anda bisa download sample programnya (beserta Sql Code utk database) di akhir postingan.

Logika programnya sederhana, awalnya memilih gambar melalui tombol Load Picture lalu save datanya ke database dengan tombol Save Picture.

Berikut adalah contoh Load File Image :
 OpenFileDialog1.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|GIF Files (*.gif)|*.gif|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|TIFF Files (*.tiff)|*.tiff"  
 If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then  
    PictureBox1.Image = New Bitmap(OpenFileDialog1.FileName)  
    PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage  
 End If  

dan berikut untuk Save (Nama file & Path) ke Database :
 Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tbl_img (gambar, alamat) VALUES ('" & TextBoxFileName.Text & "', '" & PathFile & "')", koneksi)  
 cmd.CommandType = CommandType.Text  
 Dim DReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)  
Untuk mempermudah anda bisa Download Program disini

Jangan lupa untuk mengganti nama server SQL Servernya dengan nama server SQL Server di komputer / laptop anda.


Sumber : http://aplikasivbnet.blogspot.com/2011_06_01_archive.html

Jumat, 27 April 2012

How to: Save Dataset Changes to a Database


.NET Framework 2.0

Sumber : http://msdn.microsoft.com/en-us/library/xzb1zw3x(v=vs.80).aspx
After the data in your dataset has been modified and validated, you probably want to send the updated data back to a database. In order to send the modified data to a database, you call the Update method of a TableAdapter or data adapter. The adapter's Update method updates a single data table and executes the correct command (INSERT, UPDATE, or DELETE) based on the RowState of each data row in the table.
NoteNote
Because attempting to update a data source with the contents of a dataset can result in errors, you should place the code that calls the adapter's Update method inside of a try/catch block.
The exact procedure to update a data source can vary depending on your business needs, but your application should include the following steps:
  1. Execute code that attempts sending updates to the database within a try/catch block.
  2. If an exception is caught, locate the data row that caused the error. For more information, see How to: Locate Rows that Have Errors.
  3. Reconcile the problem in the data row (programmatically if possible, or by presenting the invalid row to the user for modification), and then reattempt the update (HasErrors property, GetErrors method).