Hello,
I have downloaded codesmith and i wonder how i can use to generate DAL classes. I found that most of codesmith templates are in (csharp) which i truely dont like. I want to generate a class in vb.net similar to this one. Create public properties for the fields in the database and initialize them with default values: for example dim x as string="", dim y as integer=0
+ the class should include select insert update and delete functions. the select statements return the results as list (of t) + table. I dont know which template i should use or extend? if anybody has a code smith vb.net template with similar functionality i would be more than happy if he can upload it somewhere. I am connecting to sql server 2005. Thanks
here is an example:
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Collections
Imports System.Data
Public Class Customers
Public Sub New()
End Sub
Dim database As New Database
#Region "DataBase Variables"
Private _Found As Boolean
Private _CustomerId As Integer = 0
Private _UserName As String = ""
Private _Password As String = ""
Private _Name As String = ""
#End Region
#Region "User Variables"
#End Region
#Region "User Properties"
#End Region
#Region " DataBase Properties"
Public Property Found() As Boolean
Get
Return _Found
End Get
Set(ByVal value As Boolean)
_Found = value
End Set
End Property
'CustomerId
Public Property CustomerId() As Integer
Get
Return _CustomerId
End Get
Set(ByVal value As Integer)
_CustomerId = value
End Set
End Property
'UserName
Public Property UserName() As String
Get
Return _UserName
End Get
Set(ByVal value As String)
_UserName = value
End Set
End Property
'Password
Public Property Password() As String
Get
Return _Password
End Get
Set(ByVal value As String)
_Password = value
End Set
End Property
'Name
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
#End Region
#Region "User Methods"
#End Region
#Region "DataBase Methods"
'Save
Public Function Save() As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Method : '
'Created By : '
'Date: '
'Parameters: '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Dim Command As New System.Data.SqlClient.SqlCommand
Command.Parameters.AddWithValue("@CustomerId", CustomerId)
Command.Parameters.AddWithValue("@UserName", UserName)
Command.Parameters.AddWithValue("@Password", Password)
Command.Parameters.AddWithValue("@Name", Name)
Command.CommandText = "Insert Into Customers(CustomerId,UserName,Password,Name) Values(@CustomerId,@UserName,@Password,@Name)"
database.ExecuteQry(Command)
Return "True"
Catch ex As Exception
Return ex.Message
End Try
End Function
'GetDataTable
Public Overloads Function GetDataTable() As DataTable
Dim Command As New System.Data.SqlClient.SqlCommand
Dim Tbl As New DataTable
Dim Row As DataRow
Dim Reader As System.Data.SqlClient.SqlDataReader
Tbl.Columns.Add("CustomerId")
Tbl.Columns.Add("UserName")
Tbl.Columns.Add("Password")
Tbl.Columns.Add("Name")
Command.CommandText = "Select * from Customers "
Reader = database.DisplayQry(Command)
Try
While Reader.Read
Row = Tbl.NewRow
Row("CustomerId") = Reader("CustomerId").ToString
Row("UserName") = Reader("UserName").ToString
Row("Password") = Reader("Password").ToString
Row("Name") = Reader("Name").ToString
Tbl.Rows.Add(Row)
End While
Catch ex As Exception
End Try
Reader.Close()
Return Tbl
End Function
Public Sub UserLogin()
Dim command As New SqlCommand
command.Parameters.AddWithValue("@username", username)
command.Parameters.AddWithValue("@password", password)
command.CommandText = "select * from Customers where username=@username and password=@password"
InitData(database.DisplayQry(command))
End Sub
'Select All
Public Function SelectAll() As List(Of Customers)
Dim Command As New System.Data.SqlClient.SqlCommand
Dim Lst As New list(Of Customers)
Dim Reader As System.Data.SqlClient.SqlDataReader
Dim Var As Customers
Command.CommandText = "Select * from Customers "
Reader = database.DisplayQry(Command)
Try
While Reader.read
Var = New Customers
Var.CustomerId = Reader("CustomerId").ToString
Var.UserName = Reader("UserName").ToString
Var.Password = Reader("Password").ToString
Var.Name = Reader("Name").ToString
lst.Add(Var)
End While
Catch ex As Exception
End Try
Reader.close()
Return lst
End Function
'Display
Public Sub Display()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Method : '
'Created By : '
'Date: '
'Parameters: '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Dim Command As New System.Data.SqlClient.SqlCommand
Command.CommandText = "Select * from Customers "
InitData(database.DisplayQry(Command))
Catch ex As Exception
End Try
End Sub
'Fill Data
Private Sub InitData(ByVal Reader As System.Data.SqlClient.SqlDataReader)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Method : '
'Created By : '
'Date: '
'Parameters: '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Found = False
While Reader.Read
CustomerId = Reader("CustomerId").ToString
UserName = Reader("UserName").ToString
Password = Reader("Password").ToString
Name = Reader("Name").ToString
Found = True
End While
Catch ex As Exception
Found = False
End Try
Reader.Close()
End Sub
'Update
Public Function Update() As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Method : '
'Created By : '
'Date: '
'Parameters: '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Dim Command As New System.Data.SqlClient.SqlCommand
Command.Parameters.AddWithValue("@CustomerId", CustomerId)
Command.Parameters.AddWithValue("@UserName", UserName)
Command.Parameters.AddWithValue("@Password", Password)
Command.Parameters.AddWithValue("@Name", Name)
Command.CommandText = "Update Customers Set CustomerId=@CustomerId,UserName=@UserName,Password=@Password,Name=@Name"
database.ExecuteQry(Command)
Return "True"
Catch ex As Exception
Return ex.Message
End Try
End Function
'Delete
Public Sub Delete()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Method : '
'Created By : '
'Date: '
'Parameters: '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Try
Dim Command As New System.Data.SqlClient.SqlCommand
Command.CommandText = "Delete from Customers "
database.ExecuteQry(Command)
Catch ex As Exception
End Try
End Sub
#End Region
End Class