G
Graham Blandford
Hi guys,
Wonder if anyone can help a newbie in VB.net.
I've done a substantial amount of work in VB6 and to promote code use, I
lent some methodology from some other OOP RAD tools - but I'm having a
probem figuring out an approach in VB.Net...
In VB6, I would, for each database table, create a 'class module', which
contained all of the necessary database functions I required.
So, e.g.
I would have a class module called 'tcProvinceState', which would handle all
of my database IO for the ProvinceState table.
In each class module, I would have functions such as 'DoSelect',
'DoSelectRow', 'InsertRow', 'UpdateRow' and 'DeleteRow'.
Here are some examples of the type of thing I currently do;
Public Function Do_Select(pSearchString As String) As ADODB.Recordset
' Select all records from the table
sSQL = Do_Select_Body(pSearchString) 'Get the body
sSQL = sSQL & " ORDER BY PS.PROVINCE_STATE_NAME"
Set Do_Select = clsSQL.Perform_SQL(sSQL)
End Function
Private Function Do_Select_Body(ByRef pSearchString As String) As String
' Get the select body
sSQL = "SELECT"
sSQL = sSQL & " PS.ID"
sSQL = sSQL & ", PS.CODE"
sSQL = sSQL & ", PS.PROVINCE_STATE_NAME"
sSQL = sSQL & ", PS.COUNTRY"
sSQL = sSQL & ", PS.GMT_ZONE"
sSQL = sSQL & ", PS.TAX_GROUP_ID"
sSQL = sSQL & ", TG.CODE AS TAX_GROUP_CODE"
sSQL = sSQL & ", TG.DESCRIPTION AS TAX_GROUP_DESCRIPTION"
sSQL = sSQL & ", PS.FREIGHT_ADJUSTMENT"
sSQL = sSQL & ", PS.RECORD_CREATED"
sSQL = sSQL & ", PS.RECORD_UPDATED"
sSQL = sSQL & ", PS.USER_ID"
sSQL = sSQL & ", EL.USER_NAME AS EMPLOYEE_USER_NAME"
sSQL = sSQL & ", EL.FIRST_NAME AS EMPLOYEE_FIRST_NAME"
sSQL = sSQL & ", EL.LAST_NAME AS EMPLOYEE_LAST_NAME"
sSQL = sSQL & " FROM (("
sSQL = sSQL & "PROVINCE_STATE AS PS"
sSQL = sSQL & " LEFT JOIN EMPLOYEE_LOGIN AS EL ON PS.USER_ID = EL.ID)"
sSQL = sSQL & " LEFT JOIN TAX_GROUP AS TG ON PS.TAX_GROUP_ID = TG.ID)"
sSQL = sSQL & " WHERE 1=1"
Build_Search pSearchString
Do_Select_Body = sSQL
End Function
Public Function Insert_Row(ByRef rsRow As ADODB.Recordset, pID As Long) As
Boolean
' Insert row into the table;
Dim sErrors As String
Dim sDate As Variant
sDate = Format(Now(), "d-mmm-yyyy")
' Use Stored Procedure (SQL Server 2000)
' Initialize the input & output parameters;
Set cStoredProcedure = New ADODB.Command
Set cParam = New ADODB.Parameter
With cStoredProcedure
Set cParam = .CreateParameter("@CODE", adVarChar, adParamInput, 4,
rsRow.Fields("CODE"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@PROVINCE_STATE_NAME", adVarChar,
adParamInput, 30, rsRow.Fields("PROVINCE_STATE_NAME"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@COUNTRY", adVarChar, adParamInput,
20, rsRow.Fields("COUNTRY"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@GMT_ZONE", adSmallInt, adParamInput,
5, rsRow.Fields("GMT_ZONE"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@TAX_GROUP_ID", adInteger,
adParamInput, 8, rsRow.Fields("TAX_GROUP_ID"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@FREIGHT_ADJUSTMENT", adCurrency,
adParamInput, 8, rsRow.Fields("FREIGHT_ADJUSTMENT"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@USER_ID", adInteger, adParamInput,
8, glngUserID)
.Parameters.Append cParam
Set cParam = .CreateParameter("@ID", adInteger, adParamOutput, 8,
pID)
.Parameters.Append cParam
sSQL = "SP_PROVINCE_STATE_INSERT"
clsSQL.Perform_StoredProcedure sSQL, cStoredProcedure, sErrors
End With
' Check Errors
If Len(sErrors) Then
Display_Errors "Insert Failed", sErrors
Insert_Row = False
Exit Function
End If
' Read any output parameters
pID = cStoredProcedure.Parameters("@ID").Value
Insert_Row = True
End Function
With this method, creating a new table and its related 'class' is quite
simple.
Now, although this methodology doesn't have true inheritance, I get some
code re-use and it provided a very good platform for my DB applications.
However, I am now somewhat confused with regards to VB.Net.
..Net would appear to give me some native functionality for things such as
selects, inserts and deletes. I can see how inheritance may truly help me by
allowing me to create a 'master class', but I'm not sure how I would
implement this? - Should I be looking at writing similar code? or does .Net
offer me another option? Ideally I would like to create a visual class such
as a dataset - but from what I can see - this has to be copied from form to
form as required - not inherited.
Anyway, if anyone has any suggestioms, or can point me in the right
direction, I'd very much appreciate it.
All the best,
Graham
Wonder if anyone can help a newbie in VB.net.
I've done a substantial amount of work in VB6 and to promote code use, I
lent some methodology from some other OOP RAD tools - but I'm having a
probem figuring out an approach in VB.Net...
In VB6, I would, for each database table, create a 'class module', which
contained all of the necessary database functions I required.
So, e.g.
I would have a class module called 'tcProvinceState', which would handle all
of my database IO for the ProvinceState table.
In each class module, I would have functions such as 'DoSelect',
'DoSelectRow', 'InsertRow', 'UpdateRow' and 'DeleteRow'.
Here are some examples of the type of thing I currently do;
Public Function Do_Select(pSearchString As String) As ADODB.Recordset
' Select all records from the table
sSQL = Do_Select_Body(pSearchString) 'Get the body
sSQL = sSQL & " ORDER BY PS.PROVINCE_STATE_NAME"
Set Do_Select = clsSQL.Perform_SQL(sSQL)
End Function
Private Function Do_Select_Body(ByRef pSearchString As String) As String
' Get the select body
sSQL = "SELECT"
sSQL = sSQL & " PS.ID"
sSQL = sSQL & ", PS.CODE"
sSQL = sSQL & ", PS.PROVINCE_STATE_NAME"
sSQL = sSQL & ", PS.COUNTRY"
sSQL = sSQL & ", PS.GMT_ZONE"
sSQL = sSQL & ", PS.TAX_GROUP_ID"
sSQL = sSQL & ", TG.CODE AS TAX_GROUP_CODE"
sSQL = sSQL & ", TG.DESCRIPTION AS TAX_GROUP_DESCRIPTION"
sSQL = sSQL & ", PS.FREIGHT_ADJUSTMENT"
sSQL = sSQL & ", PS.RECORD_CREATED"
sSQL = sSQL & ", PS.RECORD_UPDATED"
sSQL = sSQL & ", PS.USER_ID"
sSQL = sSQL & ", EL.USER_NAME AS EMPLOYEE_USER_NAME"
sSQL = sSQL & ", EL.FIRST_NAME AS EMPLOYEE_FIRST_NAME"
sSQL = sSQL & ", EL.LAST_NAME AS EMPLOYEE_LAST_NAME"
sSQL = sSQL & " FROM (("
sSQL = sSQL & "PROVINCE_STATE AS PS"
sSQL = sSQL & " LEFT JOIN EMPLOYEE_LOGIN AS EL ON PS.USER_ID = EL.ID)"
sSQL = sSQL & " LEFT JOIN TAX_GROUP AS TG ON PS.TAX_GROUP_ID = TG.ID)"
sSQL = sSQL & " WHERE 1=1"
Build_Search pSearchString
Do_Select_Body = sSQL
End Function
Public Function Insert_Row(ByRef rsRow As ADODB.Recordset, pID As Long) As
Boolean
' Insert row into the table;
Dim sErrors As String
Dim sDate As Variant
sDate = Format(Now(), "d-mmm-yyyy")
' Use Stored Procedure (SQL Server 2000)
' Initialize the input & output parameters;
Set cStoredProcedure = New ADODB.Command
Set cParam = New ADODB.Parameter
With cStoredProcedure
Set cParam = .CreateParameter("@CODE", adVarChar, adParamInput, 4,
rsRow.Fields("CODE"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@PROVINCE_STATE_NAME", adVarChar,
adParamInput, 30, rsRow.Fields("PROVINCE_STATE_NAME"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@COUNTRY", adVarChar, adParamInput,
20, rsRow.Fields("COUNTRY"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@GMT_ZONE", adSmallInt, adParamInput,
5, rsRow.Fields("GMT_ZONE"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@TAX_GROUP_ID", adInteger,
adParamInput, 8, rsRow.Fields("TAX_GROUP_ID"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@FREIGHT_ADJUSTMENT", adCurrency,
adParamInput, 8, rsRow.Fields("FREIGHT_ADJUSTMENT"))
.Parameters.Append cParam
Set cParam = .CreateParameter("@USER_ID", adInteger, adParamInput,
8, glngUserID)
.Parameters.Append cParam
Set cParam = .CreateParameter("@ID", adInteger, adParamOutput, 8,
pID)
.Parameters.Append cParam
sSQL = "SP_PROVINCE_STATE_INSERT"
clsSQL.Perform_StoredProcedure sSQL, cStoredProcedure, sErrors
End With
' Check Errors
If Len(sErrors) Then
Display_Errors "Insert Failed", sErrors
Insert_Row = False
Exit Function
End If
' Read any output parameters
pID = cStoredProcedure.Parameters("@ID").Value
Insert_Row = True
End Function
With this method, creating a new table and its related 'class' is quite
simple.
Now, although this methodology doesn't have true inheritance, I get some
code re-use and it provided a very good platform for my DB applications.
However, I am now somewhat confused with regards to VB.Net.
..Net would appear to give me some native functionality for things such as
selects, inserts and deletes. I can see how inheritance may truly help me by
allowing me to create a 'master class', but I'm not sure how I would
implement this? - Should I be looking at writing similar code? or does .Net
offer me another option? Ideally I would like to create a visual class such
as a dataset - but from what I can see - this has to be copied from form to
form as required - not inherited.
Anyway, if anyone has any suggestioms, or can point me in the right
direction, I'd very much appreciate it.
All the best,
Graham