Sub GenerateClass(ByVal xIndex As Integer)
Dim tbl As Integer
Dim MyTable As String
Dim MyPrimaryKey As String
ClassTextBox.Text = ""
LstTable.SelectedIndex = xIndex
ClassTextBox.Text = ""
SQL = "Select Object_ID From sys.Tables where name='" & LstTable.Text & "'"
Conn.Open()
CMD.CommandText = SQL
RS = CMD.ExecuteReader
RS.Read()
tbl = RS("Object_ID")
Conn.Close()
MyTable = Mid(LstTable.Text, TablePrefix.Length + 1)
MyPrimaryKey = GetPrimaryKey(MyTable)
ClassTextBox.Text = "Imports System.Data.SQLClient"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "Public Class " & MyTable
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "Dim Bind as BindingSource"
Application.DoEvents()
'--generate property
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#Region ""Properties"""
Dim MyListOfField As New Collection
SQL = "Select Name, system_type_id From sys.columns where object_id=" & tbl & " Order By column_id"
Conn.Open()
CMD.CommandText = SQL
RS = CMD.ExecuteReader
Do Until RS.Read = False
MyListOfField.Add(RS("Name") & "," & RS("system_type_id"))
Loop
Conn.Close()
Dim Arr() As String
For i As Integer = 1 To MyListOfField.Count
Arr = Split(MyListOfField(i), ",")
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "Private x" & Arr(0) & " As " & GetDataType(Arr(1))
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "Public Property " & Arr(0) & "() As " & GetDataType(Arr(1))
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Get"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "Return x" & Arr(0)
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "End Get"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Set (byval value As " & GetDataType(Arr(1)) & ")"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "x" & Arr(0) & " = value"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "End Set"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "End Property"
Next
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#End Region"
'--/
Application.DoEvents()
'--generate Standard methods
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#Region ""Standard Methods"""
'--[create SP for Get Object]
StoredProcedureTextBox.Text = "Begin Transaction"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "Create Procedure Get_" & MyTable & "_By" & MyPrimaryKey
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "(@" & MyPrimaryKey & " Int)"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "As"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & vbTab & "Select * From TBL_" & MyTable & " Where " & MyPrimaryKey & " = @" & MyPrimaryKey
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "Return"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf
'--[]/
'--[get Object]
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "Public Function Get_" & MyTable & "_By" & MyPrimaryKey & "(ByVal My" & MyPrimaryKey & " As Integer) As " & MyTable
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim My" & MyTable & " As New " & MyTable
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Using DataTable As DataTable = ExecuteDataTable(""Get_" & MyTable & "_By" & MyPrimaryKey & """, New SqlParameter(""" & MyPrimaryKey & """, My" & MyPrimaryKey & "))"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "If DataTable IsNot Nothing AndAlso DataTable.Rows.Count > 0 Then "
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & "With DataTable.Rows(0)"
For i As Integer = 1 To MyListOfField.Count
Arr = Split(MyListOfField(i), ",")
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & "If Not IsDBNull(.Item(" & Arr(0) & ")) Then My" & MyTable & "." & Arr(0) & " = .Item(" & Arr(0) & ")"
Next
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & "End With"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "End IF"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "End Using"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Return My" & MyTable
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "End Function"
'--[]/
Application.DoEvents()
'--[create SP for Get Object]
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "Create Procedure Get_" & MyTable & "_List"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "As"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & vbTab & "Select * From TBL_" & MyTable & " Order By " & MyPrimaryKey
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "Return"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "Commit"
StoredProcedureTextBox.Text = StoredProcedureTextBox.Text & vbCrLf & "End Transaction"
'--[]/
'--[get list of object]
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "Public Function Get_" & MyTable & "_List() As List(of " & MyTable & ")"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim MyList" & MyTable & " As New List(of " & MyTable & ")"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Using DataTable As DataTable = ExecuteDataTable(""Get_" & MyTable & "_List"")"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "If DataTable IsNot Nothing AndAlso DataTable.Rows.Count > 0 Then"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & "For i As Integer = 0 To DataTable.Rows.Count - 1"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & "With DataTable.Rows(i)"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & vbTab & "Dim My" & MyTable & " As New " & MyTable
For i As Integer = 1 To MyListOfField.Count
Arr = Split(MyListOfField(i), ",")
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & vbTab & "IF Not IsDBNull(.Item(" & Arr(0) & ")) then My" & MyTable & "." & Arr(0) & " = .Item(" & Arr(0) & ")"
Next
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & vbTab & "MyList" & MyTable & ".Add(My" & MyTable & ")"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & vbTab & "End With"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & vbTab & "Next"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & vbTab & "End IF"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "End Using"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Return MyList" & MyTable
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "End Function"
'--[]/
Application.DoEvents()
'--[Add Data sub routine]
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "Public Sub Add_" & MyTable & "()"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim QueryCollection" & MyTable & " As New Collection"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim MyQueryTransaction As QueryTransaction = QueryTransaction_Add()"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "QueryCollection" & MyTable & ".Add(MyQueryTransaction)"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim Result As String = DAC.SaveTransaction(QueryCollection)"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "End Sub"
'--[]/
'--[Update Data sub routine]
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "Public Sub Update_" & MyTable & "()"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim QueryCollection" & MyTable & " As New Collection"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim MyQueryTransaction As QueryTransaction = QueryTransaction_Update()"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "QueryCollection" & MyTable & ".Add(MyQueryTransaction)"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbTab & "Dim Result As String = DAC.SaveTransaction(QueryCollection)"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & "End Sub"
'--[]/
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#End Region"
'--/
' create custom method region
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#Region ""Custom Methods"""
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & ""
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "#End Region"
ClassTextBox.Text = ClassTextBox.Text & vbCrLf & vbCrLf & "End Class "
File.WriteAllText(Application.StartupPath & "\Generated Class\" & MyTable & ".vb", ClassTextBox.Text)
End Sub
Demikian contoh sederhana pembuatan code generator, anda tentunya dapat menambahkan sendiri fungsi - fungsi yang anda butuhkan, semoga bermanfaat.