I have am trying to write a class that will perform Selection, Insert, Update and Delete. I copied the ProductInfo.
vb class from chapter 10 but I don't understand a lot of what it is I am reading. Nor why the author wrote the code as he or she did. My attempt to convert what I dont understand to what I need is not working. Thus far I have the following:
Option Explicit On
Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Data.OleDb
Public Class clPerfromanceMeasures
#Region "Class fields"
'Class Field Definitions the attributes follow"
Private m_PerformanceMeasures As SortedList
Private mSqlConnStr As String
Private Const LOADPERFORMANCEMEASURESSTR As String = "SELECT PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode FROM PerformanceMeasures ORDER BY TrackingDivisionCode;"
#End Region
#Region "Class Properties"
Public ReadOnly Property SqlConnStr() As String
Get
Return mSqlConnStr
End Get
End Property
Public Property PerformanceMeasures() As SortedList
Get
Return m_PerformanceMeasures
End Get
Set(ByVal Value As SortedList)
m_PerformanceMeasures = Value
End Set
End Property
Private Sub LoadPerformanceMeasures(ByVal SqlDR As OleDbDataReader)
Try
While
Dim PerfNum As Integer = 0
Dim TrackingDivisionCode As String = ""
Dim PerformanceMeasures As String = ""
Dim GroupCode As String = ""
If Not SqlDR.IsDBNull(0) Then
PerfNum = SqlDR.GetInt32(0)
End If
If Not SqlDR.IsDBNull(1) Then
TrackingDivisionCode = SqlDR.GetString(1)
End If
If Not SqlDR.IsDBNull(2) Then
PerformanceMeasures = SqlDR.GetString(21)
End If
If Not SqlDR.IsDBNull(3) Then
GroupCode = SqlDR.GetString(3)
End If
m_PerformanceMeasures.Add(PerfNum, New PerformanceMeasure(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode))
End While
SqlDR.NextResult()
Catch SqlExc As OleDbException
Throw SqlExc
Catch Exc As Exception
Throw Exc
End Try
End Sub
Private Function PrepareStringForSql(ByVal val As String) As String
val = val.Trim()
val = val.Replace("'", "''")
Return val
End Function
Private Function BooleanToSqlBit(ByVal Flag As Boolean) As String
If Flag Then
Return "1"
Else
Return "0"
End If
End Function
Private Function ValidateBusinessRules(ByVal PerfNum As Integer, _
ByVal TrackingDivisionCode As String, _
ByVal PerformanceMeasures As String, _
ByVal GroupCode As String) As Boolean
If PerformanceMeasures.Length = 0 Then
Throw New Exception("A Performance Measure is required." & PerfNum.ToString())
End If
If PerformanceMeasures.Length > 250 Then
Throw New Exception("A Performance Measure is less than 250 characters." & PerfNum.ToString())
End If
End Function
#End Region
#Region "Public Class Methods"
Public Sub LoadFromDatabase()
Dim conn As New OleDbConnection(mSqlConnStr)
Dim cmd As OleDbCommand
Dim sqlDR As OleDbDataAdapter
If m_PerformanceMeasures.Count > 0 Then
m_PerformanceMeasures.Clear()
End If
Try
If mSqlConnStr.Length = 0 Then
Throw New Exception("SQL Connection string cannot be zero-length.")
End If
conn.Open()
cmd = New OleDbCommand(LOADPERFORMANCEMEASURESSTR, conn)
cmd.CommandType = CommandType.Text
sqlDR = cmd.ExecuteReader(CommandBehavior.CloseConnection)
LoadPerformanceMeasures(sqlDR)
sqlDRClose()
Catch SqlExc As OleDbException
Throw SqlExc
Catch Exc As Exception
Throw Exc
Finally
If Not conn Is Nothing Then
If conn.State <> ConnectionState.Closed Then
If Not cmd Is Nothing Then
cmd.Cancel()
End If
conn.Close()
End If
End If
End Try
End Sub
Public Sub LoadFromDatabase(ByVal SqlConnStr As String)
If SqlConnStr.Length > 0 Then
mSqlConnStr = SqlConnStr
End If
Try
LoadFromDatabase()
Catch SqlExc As OleDbException
Throw SqlExc
Catch Exc As Exception
Throw Exc
End Try
End Sub
Public Function AddPerformanceMeasure(ByVal PerfNum As Integer, _
ByVal TrackingDivisionCode As String, _
ByVal PerformanceMeasures As String, _
ByVal GroupCode As String) As Boolean
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim perform As PerformanceMeasure
Dim newid As Integer
If mSqlConnStr.Length = 0 Then
Throw New Exception("Sql connection string cannot be zero-length")
End If
Try
If ValidateBusinessRules(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode) Then
cmd = New OleDbCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO PerformanceMeasures(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode) values & _"
(" & _
"'" & PrepareStringForSql(PerfNum.ToString()) & "',"& _
TrackingDivisionCode.ToString() & "," & _
PerformanceMeasures.ToString() & "," & _
GroupCode.ToString() & ");"
End If
End Try
End Function
#End Region
End Class
I stopped at this point. My database consists of three table the table this class creation is referring to contains, an autonumber and text data types.
I would TREMENDOUSLY appreciated direction on this! If there is a simple way to solve this that would be even better. I would like to open the databse, insert new records, close the database, minor validation and be done with it and it is not happening for me.
Thank You.
GailCG:(