| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 936 人关注过本帖
标题:数据库名.表名形成的TREEVIEW
只看楼主 加入收藏
wuyumin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-3-28
收藏
 问题点数:0 回复次数:6 
数据库名.表名形成的TREEVIEW

将数据库名绑定到TREEVIEW内后,想在TREEVIEW底下的数据库下绑定表名(指定:用户表,系统表)
如:

数据库名
用户表
表名
系统表
表名

搜索更多相关主题的帖子: 表名 数据库 TREEVIEW 绑定 用户表 
2006-03-28 14:00
wuyumin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-3-28
收藏
得分:0 

高手们,能否相告一下,我有急用,求救!求救!


送星星千百颗...愿生命活得精彩灿烂...祝你快乐 ^_^
2006-03-28 15:25
feeling
Rank: 4
来 自:福建厦门
等 级:贵宾
威 望:10
帖 子:105
专家分:0
注 册:2006-3-29
收藏
得分:0 

关键突破口:
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


互相学习! 不要放弃!时刻相信自己! 信心,是我们撑起世界的\'武器\'! 只怕你不肯学,而不怕自己学不会!
2006-03-29 21:46
feeling
Rank: 4
来 自:福建厦门
等 级:贵宾
威 望:10
帖 子:105
专家分:0
注 册:2006-3-29
收藏
得分:0 

可以再修改一下,就可以达到你的要求了:
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


互相学习! 不要放弃!时刻相信自己! 信心,是我们撑起世界的\'武器\'! 只怕你不肯学,而不怕自己学不会!
2006-03-29 22:16
wuyumin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-3-28
收藏
得分:0 

太感谢了,我得好好学习学习了,我太差劲了,呵呵。


送星星千百颗...愿生命活得精彩灿烂...祝你快乐 ^_^
2006-03-29 22:43
wuyumin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-3-28
收藏
得分:0 
如果要在表底下显示相关列名呢?那又要怎么做才可以达到这样呢。

送星星千百颗...愿生命活得精彩灿烂...祝你快乐 ^_^
2006-03-29 22:48
feeling
Rank: 4
来 自:福建厦门
等 级:贵宾
威 望:10
帖 子:105
专家分:0
注 册:2006-3-29
收藏
得分:0 

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


互相学习! 不要放弃!时刻相信自己! 信心,是我们撑起世界的\'武器\'! 只怕你不肯学,而不怕自己学不会!
2006-03-30 22:13
快速回复:数据库名.表名形成的TREEVIEW
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042995 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved