init
This commit is contained in:
129
app/BrainpoolXmlParser/DB.vb
Normal file
129
app/BrainpoolXmlParser/DB.vb
Normal file
@@ -0,0 +1,129 @@
|
||||
Imports System.Data.SqlClient
|
||||
|
||||
Public Class DB
|
||||
Private _connstring
|
||||
Private _db
|
||||
|
||||
Public FormId
|
||||
Public ControlId
|
||||
Public RecordId
|
||||
Public Username As String = "BrainpoolImporter"
|
||||
|
||||
Public Sub New(connectionString As String, dbname As String)
|
||||
_connstring = connectionString
|
||||
_db = dbname
|
||||
End Sub
|
||||
|
||||
Public Function formatTable(table As String) As String
|
||||
Return String.Format("[{0}].[dbo].[{1}]", _db, table)
|
||||
End Function
|
||||
|
||||
Public Function GetFormId() As Integer
|
||||
Dim sql = String.Format("SELECT GUID FROM {0} WHERE NAME = '{1}'", Me.formatTable("TBPMO_FORM"), "Marken")
|
||||
Return Me.Scalar(sql)
|
||||
End Function
|
||||
|
||||
Public Function GetControlId(formId As Integer, Optional name As String = "ApplicationNumber") As Integer
|
||||
Dim sql = String.Format("SELECT GUID FROM {0} WHERE NAME = '{1}' AND FORM_ID = {2}", Me.formatTable("TBPMO_CONTROL"), name, formId)
|
||||
Return Me.Scalar(sql)
|
||||
End Function
|
||||
|
||||
Public Function GetProperty(m As Mark, propertyName As String) As Object
|
||||
Dim value = CallByName(m, propertyName, CallType.Get)
|
||||
If (value = Nothing) Then
|
||||
value = DBNull.Value
|
||||
End If
|
||||
|
||||
Return value
|
||||
End Function
|
||||
|
||||
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)
|
||||
End Function
|
||||
|
||||
Public Function InsertRecord(formId As Integer)
|
||||
Dim sql = String.Format("INSERT INTO {0} (FORM_ID, ADDED_WHO) VALUES ({1}, '{2}')", Me.formatTable("TBPMO_RECORD"), formId, Username)
|
||||
Me.NonQuery(sql)
|
||||
Return Me.Scalar("SELECT MAX(GUID) FROM TBPMO_RECORD")
|
||||
End Function
|
||||
|
||||
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)
|
||||
Me.NonQuery(sql)
|
||||
Return Me.Scalar(String.Format("SELECT MAX(GUID) FROM {0} WHERE CONTROL_ID = {1}", table, controlId))
|
||||
End Function
|
||||
|
||||
Public Sub UpdateValue(controlId As Integer, recordId As Integer, value As Object)
|
||||
Dim table = Me.formatTable("TBPMO_CONTROL_VALUE")
|
||||
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)
|
||||
|
||||
Return value = valueToCompare
|
||||
End Function
|
||||
|
||||
Public Function Scalar(queryString As String)
|
||||
Dim conn As New SqlConnection(_connstring)
|
||||
Dim cmd As New SqlCommand(queryString, conn)
|
||||
Dim result
|
||||
|
||||
Try
|
||||
conn.Open()
|
||||
|
||||
result = cmd.ExecuteScalar()
|
||||
conn.Dispose()
|
||||
conn.Close()
|
||||
|
||||
Return result
|
||||
Catch ex As Exception
|
||||
Console.WriteLine("Error while executing SQL Scalar-Query ({0}): {1}", queryString, ex.Message)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
|
||||
Public Function NonQuery(queryString As String)
|
||||
Dim conn As New SqlConnection(_connstring)
|
||||
Dim cmd As New SqlCommand(queryString, conn)
|
||||
|
||||
Try
|
||||
conn.Open()
|
||||
|
||||
cmd.ExecuteNonQuery()
|
||||
conn.Dispose()
|
||||
conn.Close()
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Console.WriteLine("Error while executing SQL Query ({0}): {1}", queryString, ex.Message)
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function QueryTable(queryString As String) As DataTable
|
||||
Try
|
||||
Dim conn As New SqlConnection(_connstring)
|
||||
Dim cmd As New SqlCommand(queryString, conn)
|
||||
Dim adapter As New SqlDataAdapter(cmd)
|
||||
Dim dt As New DataTable()
|
||||
|
||||
conn.Open()
|
||||
adapter.Fill(dt)
|
||||
conn.Dispose()
|
||||
conn.Close()
|
||||
|
||||
Return dt
|
||||
Catch ex As Exception
|
||||
Console.WriteLine("Error while executing SQL Non-Query ({0}): {1}", queryString, ex.Message)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user