Subscribe:

Saturday, November 14, 2009

Membuat Form Koneksi Database dengan VB 2005


Untuk membuat suatu aplikasi agar bisa dijalankan pada sebuah jaringan diperlukan sebuah form koneksi agar programmer tidak perlu lagi membuka source code aplikasi untuk merubah Connection String-nya. Hal ini sangat berguna jika aplikasi dijalankan pada komputer yang berlainan dengan database. Berikut ini saya membuat form untuk melakukan koneksi ke-database SQL Server 2000 dengan menggunakan Visual Studio 2005 dan koneksi yang dibuat disimpan pada file notepad dengan extention ".ini". Dalam hal ini saya juga menggunakan module agar bisa diakses disemua form nantinya.







Berikut source code-nya :



Source pada module


Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Module mdlConnection
Public sqlConn As New SqlConnection
Public sqlCmd As New SqlCommand
Public DA As New SqlDataAdapter
Public DS As New DataSet
Public SR As StreamReader
Public SW As StreamWriter
Private dbServer, dbUserName, _
dbPassword, dbDatabase As String
Public myPath As String = _
AppDomain.CurrentDomain.BaseDirectory
Private Sub winConnString()
sqlConn.ConnectionString = _
"Integrated Security=SSPI;" & _
"Persist Security Info=False;" & _
"Initial Catalog=" & dbDatabase & _
";Data Source=" & dbServer
End Sub
Private Sub ConnString()
sqlConn.ConnectionString = _
"Persist Security Info=False;User ID=" & _
dbUserName & ";Pwd=" & dbPassword & _
";Initial Catalog=" & dbDatabase & _
";Data Source=" & dbServer
End Sub
Public Sub OpenConn()
sqlConn.Close()

dbServer = ""
dbUserName = ""
dbPassword = ""
dbDatabase = ""

Try
SR = File.OpenText(myPath & "Connection.ini")
Do While SR.Peek <> -1
dbServer = SR.ReadLine.Trim
dbUserName = SR.ReadLine.Trim
dbPassword = SR.ReadLine.Trim
dbDatabase = SR.ReadLine.Trim
Loop
SR.Close()
If dbUserName = "" And dbPassword = "" Then
winConnString()
Else
ConnString()
End If
sqlCmd.CommandType = CommandType.Text
sqlCmd.Connection = sqlConn
sqlConn.Open()
Catch ex As Exception
MessageBox.Show("Connection Failed", _
"Connection Error", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
frmConnection.ShowDialog()
End Try
End Sub
Public Function tryConnection(ByVal chxWin As CheckBox, _
ByVal dServer As String, ByVal dUserName As String, _
ByVal dPassword As String, ByVal dDatabase As String) _
As Boolean

dbServer = dServer
dbUserName = dUserName
dbPassword = dPassword
dbDatabase = dDatabase

If chxWin.Checked = True Then
winConnString()
Else
ConnString()
End If
Try
sqlConn.Open()
sqlConn.Close()
Return True
Catch ex As Exception
sqlConn.Close()
Return False
End Try
End Function
Public Function saveConn(ByVal dServer As String, _
ByVal dUserName As String, ByVal dPassword As String, _
ByVal dDatabase As String) As Boolean
Try
File.Delete(myPath & "Connection.ini")
SW = File.AppendText(myPath & "Connection.ini")
With SW
.WriteLine(dServer)
.WriteLine(dUserName)
.WriteLine(dPassword)
.WriteLine(dDatabase)
End With
SW.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Module



Pada form koneksi


Public Class frmConnection

Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Private Sub chxWindows_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles chxWindows.CheckedChanged
txtUserName.Clear()
txtPassword.Clear()
If chxWindows.Checked = True Then
txtUserName.ReadOnly = True
txtPassword.ReadOnly = True
Else
txtUserName.ReadOnly = False
txtPassword.ReadOnly = False
End If

End Sub

Private Sub btnTest_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTest.Click
If tryConnection(chxWindows, _
txtServer.Text.Trim, _
txtUserName.Text.Trim, _
txtPassword.Text.Trim, _
txtDatabase.Text.Trim) = True Then
MessageBox.Show("Connection Success", "Connection Success", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Connection Failed", "Connection Failed", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSave.Click
If saveConn(txtServer.Text.Trim, txtUserName.Text.Trim, _
txtPassword.Text.Trim, txtDatabase.Text.Trim) = True Then
OpenConn()
MessageBox.Show("Connection Saved Successful", "Success", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Close()
End If
End Sub
End Class



Pembuatan form koneksi ini tidak menggunakan enkripsi untuk lebih aman dapat menggunakan enkripsi dan juga dapat disimpan pada registry.










4 comments:

Marz said...

Juni Ady hadir !!!

haha...

sorry ngejunk^^v

WillyKk said...

alow kk, sy udah bisa tapi bagaimana caranya supaya kesave ke registry ya ? trim's :)

Fariz Setiawan said...

thanks brother !! =)
ilmu anda sangat membantu sekali :)

IRFAN-FARIS said...

Untuk menyimpan ke sql bagaiamana