diff --git a/app/BrainpoolXmlParser/DB.vb b/app/BrainpoolXmlParser/DB.vb index a15bf63..64d3aac 100644 --- a/app/BrainpoolXmlParser/DB.vb +++ b/app/BrainpoolXmlParser/DB.vb @@ -14,6 +14,7 @@ Public Class DB _db = dbname End Sub +#Region "=== HELPERS ===" Public Function formatTable(table As String) As String Return String.Format("[{0}].[dbo].[{1}]", _db, table) End Function @@ -37,6 +38,20 @@ Public Class DB Return value End Function + Public Function IsEqual(controlId As Integer, recordId As Integer, valueToCompare As Object) + Dim table = Me.formatTable("TBPMO_CONTROL_VALUE") + Dim sql = String.Format("SELECT VALUE FROM {0} WHERE CONTROL_ID = {1} AND RECORD_ID = {2}", table, controlId, recordId) + Dim value = Me.Scalar(sql) + + If (IsDBNull(valueToCompare) Xor IsDBNull(value)) Then + Return False + End If + + Return value = valueToCompare + End Function +#End Region + +#Region "=== RECORD ID ===" Public Function GetRecordId(controlId As Integer, applicationNumber As String) As Integer Dim sql = String.Format("SELECT RECORD_ID FROM {0} WHERE CONTROL_ID = {1} AND VALUE = '{2}'", Me.formatTable("TBPMO_CONTROL_VALUE"), controlId, applicationNumber) Return Me.Scalar(sql) @@ -47,7 +62,9 @@ Public Class DB Me.NonQuery(sql) Return Me.Scalar("SELECT MAX(GUID) FROM TBPMO_RECORD") End Function +#End Region +#Region "=== VALUES ===" Public Function InsertValue(controlId As Integer, recordId As Integer, value As Object) Dim table = Me.formatTable("TBPMO_CONTROL_VALUE") Dim sql = String.Format("INSERT INTO {0} (CONTROL_ID, RECORD_ID, VALUE) VALUES ({1}, {2}, '{3}')", table, controlId, recordId, value) @@ -60,19 +77,9 @@ Public Class DB Dim sql = String.Format("UPDATE {0} SET VALUE = '{1}' WHERE CONTROL_ID = {2} AND RECORD_ID = {3}", table, value, controlId, recordId) Me.NonQuery(sql) End Sub - - Public Function IsEqual(controlId As Integer, recordId As Integer, valueToCompare As Object) - Dim table = Me.formatTable("TBPMO_CONTROL_VALUE") - Dim sql = String.Format("SELECT VALUE FROM {0} WHERE CONTROL_ID = {1} AND RECORD_ID = {2}", table, controlId, recordId) - Dim value = Me.Scalar(sql) - - If (IsDBNull(valueToCompare) Xor IsDBNull(value)) Then - Return False - End If - - Return value = valueToCompare - End Function - +#End Region + +#Region "=== DATABASE ===" Public Function Scalar(queryString As String) Dim conn As New SqlConnection(_connstring) Dim cmd As New SqlCommand(queryString, conn) @@ -126,5 +133,37 @@ Public Class DB Throw New Exception(String.Format("Error while executing SQL Query ({0}): {1}", queryString, ex.Message)) End Try End Function +#End Region +#Region "=== IMAGES ===" + Public Sub InsertImage(bimage As Byte(), ControlId As Integer, RecordId As Integer) + Dim table = Me.formatTable("TBPMO_CONTROL_IMAGE") + Dim sql = String.Format("INSERT INTO {0} (CONTROL_ID, RECORD_ID, IMG) VALUES (@CONTROL_ID, @RECORD_ID, @IMG)", table) + Dim conn As New SqlClient.SqlConnection(_connstring) + Dim cmd As New SqlClient.SqlCommand(sql, conn) + + cmd.Parameters.Add("@IMG", SqlDbType.Binary).Value = bimage + cmd.Parameters.Add("@CONTROL_ID", SqlDbType.Int).Value = ControlId + cmd.Parameters.Add("@RECORD_ID", SqlDbType.Int).Value = RecordId + + conn.Open() + cmd.ExecuteNonQuery() + conn.Close() + End Sub + + Public Sub UpdateImage(bimage As Byte(), ControlId As Integer, RecordId As Integer) + Dim table = Me.formatTable("TBPMO_CONTROL_IMAGE") + Dim sql = String.Format("UPDATE {0} SET IMG = @IMG WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @RECORD_ID", table) + Dim conn As New SqlClient.SqlConnection(_connstring) + Dim cmd As New SqlClient.SqlCommand(sql, conn) + + cmd.Parameters.Add("@IMG", SqlDbType.Binary).Value = bimage + cmd.Parameters.Add("@CONTROL_ID", SqlDbType.Int).Value = ControlId + cmd.Parameters.Add("@RECORD_ID", SqlDbType.Int).Value = RecordId + + conn.Open() + cmd.ExecuteNonQuery() + conn.Close() + End Sub +#End Region End Class