Jumat, 24 Desember 2010

oop vb.net

Pertama buat dulu Class untuk Object Bank

Public Class BankObj

Private m_BankID As String
Private m_BankName As String
Private m_LastTime As DateTime
Private m_LastUser As String

Public Sub New()

End Sub

Public Property BankID() As String
Get
Return m_BankID
End Get
Set(ByVal value As String)
m_BankID = value
End Set
End Property

Public Property BankName() As String
Get
Return m_BankName
End Get
Set(ByVal value As String)
m_BankName = value
End Set
End Property

Public Property LastTime() As DateTime
Get
Return m_LastTime
End Get
Set(ByVal value As DateTime)
m_LastTime = value
End Set
End Property

Public Property LastUser() As String
Get
Return m_LastUser
End Get
Set(ByVal value As String)
m_LastUser = value
End Set
End Property

End Class


Lalu Buat untuk DBFunctionnya :

Imports System.Data.SqlClient

Public Class BankDB

Public Shared Function Select_Record(ByVal bankid As String) As BankObj
Dim dbo_bank As New BankObj
Dim connection As SqlConnection = PSCDATDB.GetConnection
Dim selectStatement As String _
= "SELECT * FROM [dbo].[Bank] " _
& "WHERE [BankID] = @BankID"
Dim selectCommand As New SqlCommand(selectStatement, connection)
selectCommand.CommandType = CommandType.Text
selectCommand.Parameters.AddWithValue("@BankID", bankid)
Try
connection.Open()
Dim reader As SqlDataReader _
= selectCommand.ExecuteReader(CommandBehavior.SingleRow)
If reader.Read Then
dbo_bank.BankID = System.Convert.ToString(IIf(reader("BankID") Is DBNull.Value, "", reader("BankID")))
dbo_bank.BankName = System.Convert.ToString(IIf(reader("BankName") Is DBNull.Value, "", reader("BankName")))
dbo_bank.LastTime = System.Convert.ToDateTime(IIf(reader("LastTime") Is DBNull.Value, System.DateTime.Now, reader("LastTime")))
dbo_bank.LastUser = System.Convert.ToString(IIf(reader("LastUser") Is DBNull.Value, "", reader("LastUser")))
Else
dbo_bank = Nothing
End If
reader.Close()
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
Return dbo_bank
End Function

Public Shared Function Add(ByVal dbo_bank As BankObj) As Boolean
Dim connection As SqlConnection = PSCDATDB.GetConnection
Dim insertStatement As String _
= "INSERT [dbo].[Bank] " _
& "([BankID], [BankName], [LastTime], [LastUser]) " _
& "VALUES (@BankID, @BankName, @LastTime, @LastUser)"
Dim insertCommand As New SqlCommand(insertStatement, connection)
insertCommand.CommandType = CommandType.Text
insertCommand.Parameters.AddWithValue("@BankID", dbo_bank.BankID)
insertCommand.Parameters.AddWithValue("@BankName", dbo_bank.BankName)
insertCommand.Parameters.AddWithValue("@LastTime", dbo_bank.LastTime)
insertCommand.Parameters.AddWithValue("@LastUser", dbo_bank.LastUser)
Try
connection.Open()
Dim count As Integer = insertCommand.ExecuteNonQuery()
If count > 0 Then
Return True
Else
Return False
End If
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
End Function

Public Shared Function Update(ByVal oldDbo_Bank As BankObj, _
ByVal newDbo_Bank As BankObj) As Boolean
Dim connection As SqlConnection = PSCDATDB.GetConnection
Dim updateStatement As String _
= "UPDATE [dbo].[Bank] SET " _
& "[BankID] = @NewBankID, " _
& "[BankName] = @NewBankName, " _
& "[LastTime] = @NewLastTime, " _
& "[LastUser] = @NewLastUser " _
& "WHERE [BankID] = @OldBankID "
Dim updateCommand As New SqlCommand(updateStatement, connection)
updateCommand.CommandType = CommandType.Text
updateCommand.Parameters.AddWithValue("@NewBankID", newDbo_Bank.BankID)
updateCommand.Parameters.AddWithValue("@NewBankName", newDbo_Bank.BankName)
updateCommand.Parameters.AddWithValue("@NewLastTime", newDbo_Bank.LastTime)
updateCommand.Parameters.AddWithValue("@NewLastUser", newDbo_Bank.LastUser)
updateCommand.Parameters.AddWithValue("@OldBankID", oldDbo_Bank.BankID)
Try
connection.Open()
Dim count As Integer = updateCommand.ExecuteNonQuery()
If count > 0 Then
Return True
Else
Return False
End If
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
End Function

Public Shared Function Delete(ByVal dbo_bank As BankObj) As Boolean
Dim connection As SqlConnection = PSCDATDB.GetConnection
Dim deleteStatement As String _
= "DELETE FROM [dbo].[Bank] " _
& "WHERE [BankID] = @OldBankID "
Dim deleteCommand As New SqlCommand(deleteStatement, connection)
deleteCommand.CommandType = CommandType.Text
deleteCommand.Parameters.AddWithValue("@OldBankID", dbo_bank.BankID)
Try
connection.Open()
Dim count As Integer = deleteCommand.ExecuteNonQuery()
If count > 0 Then
Return True
Else
Return False
End If
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
End Function

End Class

klik save button langsung arahkan ke ButtonApply:

Private Sub PutDbo_BankData(ByVal dbo_bank As BankObj)
dbo_bank.BankID = System.Convert.ToString(txtBankID.Text)
dbo_bank.BankName = System.Convert.ToString(txtBankName.Text)
dbo_bank.LastTime = Now
dbo_bank.LastUser = UserID
End Sub

Private Sub ButtonApply()
dbo_bank = New BankObj
Me.PutDbo_BankData(dbo_bank)
Try
If Not BankDB.Add(dbo_bank) Then
MessageBox.Show("Save Gagal", msgCaptionDBError, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Save Sukses", msgCaptionEntry, MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.DialogResult = Windows.Forms.DialogResult.OK
End If
Me.DialogResult = Windows.Forms.DialogResult.OK
Catch ex As Exception
MessageBox.Show(ex.Message, ex.GetType.ToString)
End Try

Tidak ada komentar:

Posting Komentar