• We just launched and are currently in beta. Join us as we build and grow the community.

Displaying and Dropping the MySQL Database Tables Structures using Visual Basic.Net

thenewbie123

Laugh Riot
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
158
Likes
31
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial is a continuation of our our previous topic called “Creating MySQL Database tables using Visual Basic. Net”. This time, we're going to focus on displaying and dropping of MySQL Database Tables using Visual Basic.Net.
In this application, we need to add two buttons and a datagridview. Then, rename the first button as “btndescribe” and change the text property to “Describe”, and the other button change the name to “btndrop” and the text property to “Drop”. Next, for the Datagridview1 change the name as “dtgstructure” and place it over the first datagridview.

Take note folks, the data will be lost and could not be recovered after deleting a table. Although it is very easy to drop an existing MySQL table, but you need to be very careful while deleting any existing table.

This time, let’s start adding functionality to our “Describe” button. To do this, add the following code:

  1. Private

    Sub

    btndescribe_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btndescribe.

    Click

  2. txttblName.

    ReadOnly

    =

    True
  3. btnAddtbl.

    Visible

    =

    True
  4. btnDroptbl.

    Visible

    =

    True
  5. btnCreateTbl.

    Visible

    =

    False
  6. dtgstructure.

    BringToFront

    (

    )

  7. Dim

    sql As

    String

    =

    "DESC "

    &

    cbdb.

    Text

    &

    "."

    &

    cbtable.

    Text
  8. Dim

    publictable As

    New

    DataTable
  9. Try

  10. 'bind the connection and query
  11. With

    cmd
  12. .

    Connection

    =

    con
  13. .

    CommandText

    =

    sql
  14. End

    With

  15. da.

    SelectCommand

    =

    cmd
  16. da.

    Fill

    (

    publictable)
  17. ' publictable.Rows.Add("Please Select...")
  18. dtgstructure.

    DataSource

    =

    publictable
  19. ' dtgrd.Columns(1).Visible = False
  20. da.

    Dispose

    (

    )

  21. Catch

    ex As

    Exception
  22. MsgBox

    (

    ex.

    Message

    )

  23. End

    Try
  24. con.

    Clone

    (

    )

  25. End

    Sub

Then we can test our application by pressing “F5”. Then after clicking the “Describe” button the expected output will look like as shown below.

tblstructre_0.png


Next, for the “Drop” button. Add the following code.
This code will drop the existing table in any database.

  1. Private

    Sub

    btnDroptbl_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnDroptbl.

    Click
  2. Dim

    sql As

    String

    =

    "DROP TABLE "

    &

    cbdb.

    Text

    &

    "."

    &

    cbtable.

    Text
  3. Dim

    result As

    Integer
  4. Try
  5. con.

    Open

    (

    )
  6. With

    cmd
  7. .

    Connection

    =

    con
  8. .

    CommandText

    =

    sql
  9. result =

    cmd.

    ExecuteNonQuery
  10. If

    result >

    0

    Then
  11. MsgBox

    (

    "Error in dropping Field!"

    )
  12. Else
  13. MsgBox

    (

    cbtable.

    Text

    &

    " has Successfully dropped!"

    )
  14. With

    Me
  15. .

    txttblName

    .

    ReadOnly

    =

    True
  16. .

    btnCreateTbl

    .

    Visible

    =

    False
  17. .

    btnAddtbl

    .

    Visible

    =

    True
  18. .

    btnDroptbl

    .

    Visible

    =

    True
  19. .

    dtgStructNewTbl

    .

    SendToBack

    (

    )
  20. dtgstructure.

    Columns

    .

    Clear

    (

    )
  21. End

    With


  22. End

    If
  23. End

    With


  24. Form1_Load(

    sender, e)

  25. Catch

    ex As

    Exception
  26. MsgBox

    (

    ex.

    Message

    )
  27. End

    Try
  28. con.

    Close

    (

    )
  29. Call

    Form1_Load(

    sender, e)


  30. End

    Sub

And here’s all the code use for this application.

  1. Imports

    MySql.

    Data

    .

    MySqlClient
  2. Public

    Class

    Form1
  3. 'Represents an SQL statement or stored procedure to execute against a data source.
  4. Dim

    cmd As

    New

    MySqlCommand
  5. Dim

    da As

    New

    MySqlDataAdapter
  6. Public

    total As

    Integer
  7. Dim

    publictable As

    New

    DataTable
  8. 'declare conn as connection and it will now a new connection because
  9. 'it is equal to Getconnection Function
  10. Dim

    con As

    MySqlConnection =

    jokenconn(

    )

  11. Public

    Function

    jokenconn(

    )

    As

    MySqlConnection
  12. Return

    New

    MySqlConnection(

    "server=localhost;user id=root;password=;database="

    )
  13. End

    Function
  14. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  15. GroupBox1.

    Text

    =

    "Create new table on database "
  16. Dim

    sql As

    String

    =

    "SHOW DATABASES"
  17. Dim

    publictable As

    New

    DataTable
  18. Try

  19. 'bind the connection and query
  20. With

    cmd
  21. .

    Connection

    =

    con
  22. .

    CommandText

    =

    sql
  23. End

    With

  24. da.

    SelectCommand

    =

    cmd
  25. da.

    Fill

    (

    publictable)

  26. With

    cbdb
  27. .

    DataSource

    =

    publictable
  28. .

    DisplayMember

    =

    "Database"
  29. .

    ValueMember

    =

    "Database"
  30. End

    With

  31. da.

    Dispose

    (

    )

  32. Catch

    ex As

    Exception
  33. MsgBox

    (

    ex.

    Message

    )

  34. End

    Try
  35. con.

    Clone

    (

    )


  36. End

    Sub

  37. Private

    Sub

    btntest_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btntest.

    Click
  38. GroupBox1.

    Text

    =

    "Create new table on database "

    &

    cbdb.

    Text
  39. Dim

    sql As

    String

    =

    "SHOW TABLES FROM "

    &

    cbdb.

    Text
  40. Dim

    publictable As

    New

    DataTable
  41. Try

  42. 'bind the connection and query
  43. With

    cmd
  44. .

    Connection

    =

    con
  45. .

    CommandText

    =

    sql
  46. End

    With

  47. da.

    SelectCommand

    =

    cmd
  48. da.

    Fill

    (

    publictable)
  49. ' publictable.Rows.Add("Please Select...")
  50. With

    cbtable
  51. .

    DataSource

    =

    publictable
  52. .

    DisplayMember

    =

    "Tables_in_"

    &

    cbdb.

    Text
  53. .

    ValueMember

    =

    "Tables_in_"

    &

    cbdb.

    Text

  54. End

    With
  55. ' dtgrd.Columns(1).Visible = False
  56. da.

    Dispose

    (

    )

  57. Catch

    ex As

    Exception
  58. MsgBox

    (

    ex.

    Message

    )

  59. End

    Try
  60. con.

    Clone

    (

    )

  61. End

    Sub

  62. Private

    Sub

    btnAddtbl_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnAddtbl.

    Click

  63. txttblName.

    ReadOnly

    =

    False
  64. btnCreateTbl.

    Visible

    =

    True
  65. btnAddtbl.

    Visible

    =

    False
  66. btnDroptbl.

    Visible

    =

    False
  67. dtgstructure.

    SendToBack

    (

    )
  68. End

    Sub

  69. Private

    Sub

    btnCreateTbl_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnCreateTbl.

    Click

  70. Dim

    field As

    String
  71. Dim

    type As

    String
  72. Dim

    nlenght As

    Integer
  73. Dim

    nNull As

    String
  74. Dim

    nIndex As

    String
  75. Dim

    nAI As

    String
  76. Dim

    alltxt As

    String
  77. Dim

    result As

    Integer

  78. Dim

    tblAndDB As

    String

    =

    cbdb.

    Text

    &

    "."

    &

    txttblName.

    Text
  79. Dim

    TBLCREATE As

    String

    =

    "CREATE TABLE "

    &

    tblAndDB &

    " ( "

  80. For

    i As

    Integer

    =

    0

    To

    Me

    .

    dtgStructNewTbl

    .

    Rows

    .

    Count

    -

    2

  81. ' field.Add(Me.dtgStructNewTbl.Rows(i).Cells("n_field").Value)
  82. field =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_field"

    )

    .

    Value
  83. type =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_type"

    )

    .

    Value
  84. nlenght =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_lenght"

    )

    .

    Value
  85. nNull =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_null"

    )

    .

    Value
  86. nIndex =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_index"

    )

    .

    Value
  87. nAI =

    Me

    .

    dtgStructNewTbl

    .

    Rows

    (

    i)

    .

    Cells

    (

    "n_ai"

    )

    .

    Value

  88. alltxt +=

    field &

    " "

    &

    type &

    "("

    &

    nlenght &

    ")"

    &

    " "

    &

    nNull &

    " "

    &

    nIndex &

    " "

    &

    nAI &

    ","

    &

    vbNewLine

  89. Next
  90. Try
  91. 'it removes all the newline and whitespaces
  92. alltxt =

    alltxt.

    Substring

    (

    0

    , alltxt.

    Length

    -

    Environment.

    NewLine

    .

    Length

    )
  93. Dim

    cleanText As

    String
  94. 'it removes the the last comma ","
  95. cleanText =

    alltxt.

    Remove

    (

    alltxt.

    Length

    -

    1

    )
  96. Dim

    finalText As

    String
  97. 'combination of finalText with table name and Mysql ENGINE
  98. finalText =

    TBLCREATE &

    cleanText &

    ")ENGINE = MYISAM ;"

  99. con.

    Open

    (

    )
  100. With

    cmd
  101. .

    Connection

    =

    con
  102. .

    CommandText

    =

    finalText
  103. result =

    cmd.

    ExecuteNonQuery
  104. If

    result >

    0

    Then
  105. MsgBox

    (

    "No Table has created!"

    )
  106. Else
  107. MsgBox

    (

    txttblName.

    Text

    &

    " has created Successfully!"

    )
  108. With

    Me
  109. .

    txttblName

    .

    ReadOnly

    =

    True
  110. .

    btnCreateTbl

    .

    Visible

    =

    False
  111. .

    btnAddtbl

    .

    Visible

    =

    True
  112. .

    btnDroptbl

    .

    Visible

    =

    True
  113. .

    dtgStructNewTbl

    .

    SendToBack

    (

    )
  114. '.dtgStructNewTbl.Columns.Clear()
  115. End

    With


  116. End

    If
  117. End

    With


  118. Form1_Load(

    sender, e)

  119. Catch

    ex As

    Exception
  120. MsgBox

    (

    ex.

    Message

    )
  121. End

    Try
  122. con.

    Close

    (

    )
  123. End

    Sub

  124. Private

    Sub

    btndescribe_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btndescribe.

    Click

  125. txttblName.

    ReadOnly

    =

    True
  126. btnAddtbl.

    Visible

    =

    True
  127. btnDroptbl.

    Visible

    =

    True
  128. btnCreateTbl.

    Visible

    =

    False
  129. dtgstructure.

    BringToFront

    (

    )

  130. Dim

    sql As

    String

    =

    "DESC "

    &

    cbdb.

    Text

    &

    "."

    &

    cbtable.

    Text
  131. Dim

    publictable As

    New

    DataTable
  132. Try

  133. 'bind the connection and query
  134. With

    cmd
  135. .

    Connection

    =

    con
  136. .

    CommandText

    =

    sql
  137. End

    With

  138. da.

    SelectCommand

    =

    cmd
  139. da.

    Fill

    (

    publictable)
  140. ' publictable.Rows.Add("Please Select...")
  141. dtgstructure.

    DataSource

    =

    publictable
  142. ' dtgrd.Columns(1).Visible = False
  143. da.

    Dispose

    (

    )

  144. Catch

    ex As

    Exception
  145. MsgBox

    (

    ex.

    Message

    )

  146. End

    Try
  147. con.

    Clone

    (

    )

  148. End

    Sub

  149. Private

    Sub

    btnDroptbl_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnDroptbl.

    Click
  150. Dim

    sql As

    String

    =

    "DROP TABLE "

    &

    cbdb.

    Text

    &

    "."

    &

    cbtable.

    Text
  151. Dim

    result As

    Integer
  152. Try
  153. con.

    Open

    (

    )
  154. With

    cmd
  155. .

    Connection

    =

    con
  156. .

    CommandText

    =

    sql
  157. result =

    cmd.

    ExecuteNonQuery
  158. If

    result >

    0

    Then
  159. MsgBox

    (

    "Error in dropping Field!"

    )
  160. Else
  161. MsgBox

    (

    cbtable.

    Text

    &

    " has Successfully dropped!"

    )
  162. With

    Me
  163. .

    txttblName

    .

    ReadOnly

    =

    True
  164. .

    btnCreateTbl

    .

    Visible

    =

    False
  165. .

    btnAddtbl

    .

    Visible

    =

    True
  166. .

    btnDroptbl

    .

    Visible

    =

    True
  167. .

    dtgStructNewTbl

    .

    SendToBack

    (

    )
  168. dtgstructure.

    Columns

    .

    Clear

    (

    )
  169. End

    With


  170. End

    If
  171. End

    With


  172. Form1_Load(

    sender, e)

  173. Catch

    ex As

    Exception
  174. MsgBox

    (

    ex.

    Message

    )
  175. End

    Try
  176. con.

    Close

    (

    )
  177. Call

    Form1_Load(

    sender, e)


  178. End

    Sub
  179. End

    Class

After reviewing all the codes above, you can now test your application by pressing “F5”.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top