Guest User

Untitled

a guest
Feb 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class VBMySQL
  4. Public Property Server = "xxx.xxx.xxx.xxx"
  5. Public Property UserName = "user"
  6. Public Property Password = "password"
  7. Public Property Database = "db"
  8.  
  9. Public Function Connect_Server() As String
  10. Connect_Server = "server=" & Server & ";" & "userid=" & UserName & ";" & "password=" & Password & ";" & "database=" & Database
  11. Return Connect_Server
  12. End Function
  13. Public Function Retrieve_Row(SQLStatement As String) As ArrayList
  14. Dim Result As New ArrayList
  15. Dim Reader As MySqlDataReader
  16. Dim MySQL_Connection As New MySqlConnection
  17. Dim MySQL_Command As New MySqlCommand
  18. Try
  19. MySQL_Connection.ConnectionString = Connect_Server()
  20. MySQL_Command.CommandText = SQLStatement
  21. MySQL_Command.Connection = MySQL_Connection
  22. MySQL_Connection.Open()
  23. Reader = MySQL_Command.ExecuteReader
  24. While Reader.Read()
  25. Dim dict As New Dictionary(Of String, Object)
  26. For count As Integer = 0 To (Reader.FieldCount - 1)
  27. dict.Add(Reader.GetName(count), Reader(count))
  28. Next
  29. Result.Add(dict)
  30. End While
  31. Reader.Close()
  32. MySQL_Connection.Close()
  33. Catch ex As Exception
  34. Console.WriteLine("MySQL retrieve row: " & ex.Message & Err.Number)
  35. Finally
  36. MySQL_Connection.Dispose()
  37. MySQL_Connection = Nothing
  38. Reader = Nothing
  39. End Try
  40. GC.Collect()
  41. Return Result
  42. End Function
  43. Public Function Retrieve_Value(SQLStatement As String) As String
  44. Dim Result As String
  45. Dim MySQL_Connection As New MySqlConnection
  46. Dim MySQL_Command As New MySqlCommand
  47. Try
  48. MySQL_Connection.ConnectionString = Connect_Server()
  49. MySQL_Command.CommandText = SQLStatement
  50. MySQL_Command.Connection = MySQL_Connection
  51. MySQL_Connection.Open()
  52. Result = MySQL_Command.ExecuteScalar()
  53. MySQL_Connection.Close()
  54. Catch ex As Exception
  55. Console.WriteLine("MySQL retrieve value: " & ex.Message & Err.Number)
  56. Result = Nothing
  57. Finally
  58. MySQL_Connection.Dispose()
  59. MySQL_Connection = Nothing
  60. End Try
  61. GC.Collect()
  62. Return Result
  63. End Function
  64. Public Function Retrieve_Table(SQLStatement) As DataTable
  65. Dim table As New DataTable
  66. Dim Reader As MySqlDataReader
  67. Dim MySQL_Connection As New MySqlConnection
  68. Dim MySQL_Command As New MySqlCommand
  69. Try
  70. MySQL_Connection.ConnectionString = Connect_Server()
  71. MySQL_Command.CommandText = SQLStatement
  72. MySQL_Command.Connection = MySQL_Connection
  73. MySQL_Connection.Open()
  74. Reader = MySQL_Command.ExecuteReader
  75. table.Load(Reader)
  76. Reader.Close()
  77. MySQL_Connection.Close()
  78. Catch ex As Exception
  79. Console.WriteLine("MySQL retrieve table: " & ex.Message & Err.Number)
  80. table = Nothing
  81. Finally
  82. MySQL_Connection.Dispose()
  83. MySQL_Connection = Nothing
  84. Reader = Nothing
  85. End Try
  86. GC.Collect()
  87. Return table
  88. End Function
  89. Public Function Insert_Row(SQLStatement As String) As Boolean
  90. Insert_Row = False
  91. Dim MySQL_Connection As New MySqlConnection
  92. Dim MySQL_Command As New MySqlCommand
  93. Try
  94. MySQL_Connection.ConnectionString = Connect_Server()
  95. MySQL_Command.CommandText = SQLStatement
  96. MySQL_Command.Connection = MySQL_Connection
  97. MySQL_Connection.Open()
  98. MySQL_Command.ExecuteNonQuery()
  99. MySQL_Connection.Close()
  100. Insert_Row = True
  101. Catch ex As MySqlException
  102. Console.WriteLine("MySQL insert: " & ex.Message & Err.Number)
  103. Insert_Row = False
  104. Finally
  105. MySQL_Connection.Dispose()
  106. MySQL_Connection = Nothing
  107. End Try
  108. GC.Collect()
  109. End Function
  110. Public Function Delete_Row(SQLStatement As String) As Boolean
  111. Delete_Row = False
  112. Dim MySQL_Connection As New MySqlConnection
  113. Dim MySQL_Command As New MySqlCommand
  114. Try
  115. MySQL_Connection.ConnectionString = Connect_Server()
  116. MySQL_Command.CommandText = SQLStatement
  117. MySQL_Command.Connection = MySQL_Connection
  118. MySQL_Connection.Open()
  119. MySQL_Command.ExecuteNonQuery()
  120. MySQL_Connection.Close()
  121. Delete_Row = True
  122. Catch ex As MySqlException
  123. Console.WriteLine("MySQL delete: " & ex.Message & Err.Number)
  124. Delete_Row = False
  125. Finally
  126. MySQL_Connection.Dispose()
  127. MySQL_Connection = Nothing
  128. End Try
  129. GC.Collect()
  130. End Function
  131. End Class
  132.  
  133. Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
  134. Dim CreateMySQL As New VBMySQL With {.Database = d, .Server = s, .Password = p, .UserName = u}
  135. Try
  136. CreateMySQL.Insert_Row("INSERT INTO `Test_Table`(`entry_by`, `test_data`) VALUES('" & txtEntryBy.Text & "','" & txtData.Text & "')")
  137. Catch ex As Exception
  138. Console.WriteLine("Creation error: " & ex.Message)
  139. End Try
  140. End Sub
Add Comment
Please, Sign In to add comment