简体   繁体   English

使用vb.net截断来自mySQL数据库的选定表

[英]Truncate selected tables from mySQL database using vb.net

I had populated a listbox with all my tables in the database with the query below: 我用下面的查询填充了数据库中所有表的列表框:

Dim cmd As New OdbcCommand("SHOW TABLES", con)
Dim reader As OdbcDataReader
reader = cmd.ExecuteReader
While reader.Read
allTables.Items.Add(reader(0).ToString)
End While

Using buttons i moved some table that can't be Truncated to another listBox named safeTables . 使用按钮,我移动了一些不能被Truncated表到另一个名为safeTables

Now am having two listBoxws 现在有两个listBoxws

  • allTables : List of all tables allTables :所有表的列表
  • safeTables : List of tables that can't be Truncated How can i perform the operation? safeTables :不能被Truncated的表的列表如何执行该操作? Can anyone suggest me the best method to do it? 任何人都可以建议我最好的方法吗?

Let btnTruncate be a button its click event will truncate some tables expect safeTables in the second list: now see the following code: 假设btnTruncate是一个按钮,其click事件将截断一些表,期望第二个列表中的safeTables :现在看以下代码:

Private Sub btnTruncate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTruncate.Click
    Dim tableList As List(Of String) = New List(Of String)
    Dim notDelete As List(Of String) = New List(Of String)
    tableList.AddRange(allTables.Items)
    notDelete.AddRange(tableToRemove)
    truncateTables(tableList, notDelete)
End Sub

 Public Sub truncateTables(ByVal tableToTruncate As List(Of String), ByVal safeTables As List(Of String))
        con.Open()
        For Each table As String In tableToTruncate
            If Not safeTables.Contains(table) Then
                Dim cmd As New OdbcCommand("truncate table " & table, con)
                cmd.ExecuteNonQuery()
            End If
        Next
 End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM