将数据库名绑定到TREEVIEW内后,想在TREEVIEW底下的数据库下绑定表名(指定:用户表,系统表)
如:
数据库名
用户表
表名
系统表
表名
高手们,能否相告一下,我有急用,求救!求救!
关键突破口:
Imports System.data.sqlclient
......
Public cn As New SqlConnection("data source=localhost;database=master;integrated security=true;")
Dim s As New TextBox
Dim s1 As New TextBox
Dim str As String
Dim str1 As String
Private Sub Basciemployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn.Close()
cn.Open()
Dim cmd As New SqlCommand("select name from master.dbo.sysdatabases", cn)
Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
TreeView1.Nodes.Clear()
While rd.Read
If Not IsDBNull(rd.Item(0)) Then
TreeView1.Nodes.Add(Trim(rd.Item(0)))
Else
TreeView1.Nodes.Add("none!")
End If
End While
cn.Close()
Catch sql as sqlexception
MsgBox("sql information:" & sql.message)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Try
cn.Close()
cn.Open()
Dim cmd As New SqlCommand("select name from master.dbo.sysdatabases ", cn)
Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
TreeView2.Nodes.Clear()
While rd.Read
If Not IsDBNull(rd.Item(0)) Then
TreeView2.Nodes.Add(Trim(rd.Item(0)))
Else
TreeView2.Nodes.Add("none!")
End If
End While
cn.Close()
Catch sql as sqlexception
MsgBox("sql information:" & sql.message)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Try
Dim n As TreeNode
For Each n In TreeView1.Nodes
s.Text = TreeView1.SelectedNode.Text
str = "use " & s.Text & " select name from sysobjects where OBJECTPROPERTY(id, 'IsUserTable') = 1 "
cn.Close()
cn.Open()
Dim cmd1 As New SqlCommand(str, cn)
Dim rd1 As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
TreeView1.SelectedNode.Nodes.Clear()
While rd1.Read
If Not IsDBNull(rd1.Item(0)) Then
TreeView1.SelectedNode.Nodes.Add(rd1.Item(0))
Else
TreeView1.SelectedNode.Nodes.Add("another!")
End If
End While
rd1.Close()
cn.Close()
Next
Catch sql As SqlException
MsgBox("sql information:" & sql.ToString)
MsgBox("表不能替代数据库,请点主节点,而不是子节点!")
Catch ex As Exception
MsgBox(ex.ToString)
Finally
cn.Close()
End Try
End Sub
Private Sub TreeView2_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView2.AfterSelect
Try
Dim n As TreeNode
For Each n In TreeView2.Nodes
s1.Text = TreeView2.SelectedNode.Text
str1 = "use " & s1.Text & " select name from sysobjects where OBJECTPROPERTY(id, 'IsSystemTable') = 1 "
cn.Close()
cn.Open()
Dim cmd1 As New SqlCommand(str1, cn)
Dim rd1 As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
TreeView2.SelectedNode.Nodes.Clear()
While rd1.Read
If Not IsDBNull(rd1.Item(0)) Then
TreeView2.SelectedNode.Nodes.Add(rd1.Item(0))
Else
TreeView2.SelectedNode.Nodes.Add("another!")
End If
End While
rd1.Close()
cn.Close()
Next
Catch sql As SqlException
MsgBox("sql information:" & sql.ToString)
MsgBox("表不能替代数据库,请点主节点,而不是子节点!")
Catch ex As Exception
MsgBox(ex.ToString)
Finally
cn.Close()
End Try
End Sub
可以再修改一下,就可以达到你的要求了:
imports system.data.sqlclient
......
Public cn As New SqlConnection("data source=localhost;database=master;integrated security=true;")
Dim s1 As New TextBox
Dim str1 As String
Dim str2 as String
Private Sub Basciemployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn.Close()
cn.Open()
Dim cmd As New SqlCommand("select name from master.dbo.sysdatabases", cn)
Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
TreeView1.Nodes.Clear()
While rd.Read
If Not IsDBNull(rd.Item(0)) Then
TreeView1.Nodes.Add(Trim(rd.Item(0)))
Else
TreeView1.Nodes.Add("none!")
End If
End While
cn.Close()
Catch sql as sqlexception
MsgBox("sql information:" & sql.message)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
end sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Try
Dim n As TreeNode
For Each n In TreeView1.Nodes
s1.Text = TreeView1.SelectedNode.Text
str1 = "use " & s1.Text & " select name from sysobjects where OBJECTPROPERTY(id, 'IsSystemTable') = 1 "
cn.Close()
cn.Open()
Dim cmd1 As New SqlCommand(str1, cn)
Dim rd1 As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
TreeView1.SelectedNode.Nodes.Clear()
While rd1.Read
If Not IsDBNull(rd1.Item(0)) Then
TreeView1.SelectedNode.Nodes.Add("系统表:" & rd1.Item(0))
Else
TreeView1.SelectedNode.Nodes.Add("another!")
End If
End While
rd1.Close()
cn.Close()
Next
str2 = "use " & s1.Text & " select name from sysobjects where OBJECTPROPERTY(id, 'IsUserTable') = 1 "
cn.Close()
cn.Open()
Dim cmd2 As New SqlCommand(str2, cn)
Dim rd2 As SqlDataReader = cmd2.ExecuteReader(CommandBehavior.CloseConnection)
While rd2.Read
If Not IsDBNull(rd2.Item(0)) Then
TreeView1.SelectedNode.Nodes.Add("用户表:" & rd2.Item(0))
Else
TreeView1.SelectedNode.Nodes.Add("another!")
End If
End While
rd2.Close()
cn.Close()
Catch sql As SqlException
MsgBox("sql information:" & sql.ToString)
MsgBox("表不能替代数据库,请点主节点,而不是子节点!")
Catch ex As Exception
MsgBox(ex.ToString)
Finally
cn.Close()
End Try
End Sub
Key:use master select a.name from syscolumns a, sysobjects b where (OBJECTPROPERTY(a.id, N'IsUserTable') = 1 or OBJECTPROPERTY(a.id, N'IsSystemTable') = 1)
and a.id=b.id and b.name='table_name'
Public cn As New SqlConnection("data source=localhost;database=master;integrated security=true;")
Dim s1 As New TextBox
Dim s2 As New TextBox
Dim str1 As String
Dim str2 As String
Private Sub usersInformation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn.Open()
Dim cmd As New SqlCommand("select name from sysdatabases ", cn)
Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
TreeView1.Nodes.Clear()
While rd.Read
If Not IsDBNull(rd.Item(0)) Then
TreeView1.Nodes.Add(Trim(rd.Item(0)))
Else
TreeView1.Nodes.Add("none!")
End If
End While
Catch sql As SqlException
MsgBox("sql information:" & sql.Message)
Catch ex As Exception
MsgBox("system information:" & ex.Message)
Finally
cn.Close()
End Try
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Try
Dim n As TreeNode
For Each n In TreeView1.Nodes
s1.Text = TreeView1.SelectedNode.Text
str1 = "use " & s1.Text & " select name from sysobjects where OBJECTPROPERTY(id, 'IsUserTable') = 1 or OBJECTPROPERTY(id, 'IsSystemTable') = 1 "
cn.Close()
cn.Open()
Dim cmd1 As New SqlCommand(str1, cn)
Dim rd1 As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
TreeView2.Nodes.Clear()
While rd1.Read
If Not IsDBNull(rd1.Item(0)) Then
TreeView2.Nodes.Add(rd1.Item(0))
Else
TreeView2.Nodes.Add("another!")
End If
End While
rd1.Close()
cn.Close()
Next
Catch sql As SqlException
MsgBox("sql information:" & sql.ToString)
MsgBox("表不能替代数据库,请点主节点,而不是子节点!")
Catch ex As Exception
MsgBox(ex.ToString)
Finally
cn.Close()
End Try
End Sub
Private Sub TreeView2_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView2.AfterSelect
Try
Dim n As TreeNode
For Each n In TreeView2.Nodes
s2.Text = TreeView2.SelectedNode.Text
str2 = "use " & s1.Text & " select a.name from syscolumns a, sysobjects b where (OBJECTPROPERTY(a.id, N'IsUserTable') = 1 or OBJECTPROPERTY(a.id, N'IsSystemTable') = 1) and a.id=b.id and b.name= '" & s2.Text & "'"
cn.Close()
cn.Open()
Dim cmd1 As New SqlCommand(str2, cn)
Dim rd1 As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
TreeView2.SelectedNode.Nodes.Clear()
While rd1.Read
If Not IsDBNull(rd1.Item(0)) Then
TreeView2.SelectedNode.Nodes.Add(rd1.Item(0))
Else
TreeView2.SelectedNode.Nodes.Add("another!")
End If
End While
rd1.Close()
cn.Close()
Next
Catch sql As SqlException
MsgBox("sql information:" & sql.ToString)
MsgBox("列不能替代数据表,请点主节点,而不是子节点!")
Catch ex As Exception
MsgBox(ex.ToString)
Finally
cn.Close()
End Try
End Sub