Koneksi VB.Net dengan SQL Server

Wah, lama juga ternyata gak nengok nyonyahm hehhehe. Oke dah, langsung aja capcuz…

Kali ini mau sedikit berbagi dengan pembaca blog ini tentang ‘bagaimana sih cara bikin koneksi antara aplikasi yang dibikin dengan vb.net jika kita nggunain database ato server yang beda komputer?’. Soalnya kebanyakan tutorial cuma mbahasnya bikin koneksi yang local alias server ato database-nya di satu komputer dengan aplikasinya. Lha kalo beda?? Jeng-jeng… setelah nyari-nyari, akhirnya ketemu dah, semoga bisa membantu buat yang membutuhkan hwehehhehe.

 Imports System

Imports System.Configuration

Imports System.ComponentModel

Imports Microsoft.VisualBasic

Imports System.Data.SqlClient

Public Class DbConnection

#Region "Class Variables"

'Initialization Const

'Initialization connection string

Private Shared STRING_PROVIDER As String = "Network Library=DBMSSOCN;" & _

"Data Source=10.10.249.10,1433;" & _

"Initial Catalog=nama_database;" & _

"User ID=***;" & _

"Password=****"

'Error messages

'Initialization protected variables

Protected Shared _exception As Exception

Protected dbConnection As SqlConnection

Protected dbCommand As SqlCommand

Protected dbDataAdapter As New SqlDataAdapter

Protected dbDataSet As New DataSet

'Protected _dbDataAdapter As OleDbDataAdapter

#End Region

#Region "Public Functions"

<Description("If an call returns false, then this will contain the exception")> _

Public Shared ReadOnly Property ConnException() As Exception

Get

Return _exception

End Get

End Property

Public Function GetDbCommand() As SqlCommand

dbCommand = New SqlCommand

Return dbCommand

End Function

Public Function GetDbDataAdapter() As SqlDataAdapter

Return dbDataAdapter

End Function

Public Function GetDbDataSet() As DataSet

Return dbDataSet

End Function

'*****************************************************

'* OpenDb : get resource of DbConnection

'*****************************************************

Public Function OpenDb() As Boolean

'create object OleDbConnection

Try

dbConnection = New SqlConnection()

dbConnection.ConnectionString = STRING_PROVIDER

dbConnection.Open()

Catch ex As Exception

_exception = New Exception(ex.Message.ToString)

'MsgBox("tidak ada koneksi ke database")

Return False

End Try

Return True

End Function

'*****************************************************

'* CloseDb : get resource of DbConnection

'*****************************************************

Public Function CloseDb() As Boolean

'create object OleDbConnection

Try

dbConnection.Close()

dbConnection = Nothing

Catch ex As Exception

_exception = New Exception(ex.Message.ToString)

Return False

End Try

Return True

End Function

'*****************************************************

'* ConnResource : get resource of DbConnection

'*****************************************************

Public Function ConnResource() As SqlConnection

Return dbConnection

End Function

'*****************************************************

'* ExecuteCmd : Execute Sql command

'*****************************************************

Public Function ExecuteCmd(ByVal CmdString As String) As Boolean

'Insert data

Try

'** Instantiate a command object and set the object properties

'insert data pelanggan

dbCommand = New SqlCommand

dbCommand.CommandType = CommandType.StoredProcedure

dbCommand.Connection = dbConnection

dbCommand.CommandText = CmdString

'** Open the database connection, execute the SQL command, close the

'** database connection

dbCommand.ExecuteNonQuery()

'dbCommand.Dispose()

'dbCommand = Nothing

Catch ex As Exception

_exception = New Exception(ex.Message.ToString)

'MsgBox(ex.Message.ToString)

Return False

End Try

Return True

End Function

#End Region

#Region "Shared Functions"

#End Region

#Region "Utility Functions"

#End Region

End Class

Penjelasannya ntu gini, buat class baru dengan nama DbConnection, trus ikutin aja yang imports system, itu untuk ambil system yang dibutuhkan di kelas ini. Trus Data_source itu alamat IP dari komputer yang digunakan sebagai server, bisa juga diganti dengan nama komputer. Initial_catalogue diisi dengan nama database yang akan digunakan, sedangkan username dan password diisi dengan username dan password buat konek ke SQL Servernya. Silahkan dicoba 🙂

Advertisements

One thought on “Koneksi VB.Net dengan SQL Server

Tiada jejak tanpa komentarmu :D

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s