VB中,怎么将TreeView绑定到数据库?

VB中,怎么将TreeView绑定到数据库?,第1张

'VB利用ADODB连接ACCESS数据库,执行查询、删除 *** 作

'引用Microsoft ActiveX Data Objects 2.8 Library

Dim select_id As String

Dim ConnStr As String

Private Sub Command1_Click() '刷新

GetValue

End Sub

Private Sub Form_Load()

ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=C:\你文件的路径\abc.mdbPersist Security Info=False"

TreeView1.LineStyle = tvwRootLines

TreeView1.LabelEdit = tvwManual

GetValue

End Sub

Private Sub mnu_del_Click() '菜单:删除

    DelRecord select_id

End Sub

Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

Dim nd As Node

If Button = vbRightButton Then

    Set nd = TreeView1.HitTest(x, y)

    If Not nd Is Nothing Then

        If Left(nd.Key, 1) = "R" Then

            select_id = nd.Text

            Me.PopupMenu mnupop

        End If

    End If

End If

End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)

If Left(Node.Key, 1) = "R" Then

    Text1.Text = Node.Text

    Text2.Text = Node.Child.Text

End If

End Sub

Private Sub GetValue()

Dim i As Integer

Dim Cnn As ADODB.Connection

Dim Rs As ADODB.Recordset

Dim Cnn_c As New ADODB.Command

Set Cnn = New ADODB.Connection

Set Rs = New ADODB.Recordset

Cnn.ConnectionString = ConnStr

With Cnn

    .CursorLocation = adUseClient

    .Open

End With

Cnn_c.CommandType = adCmdText

Cnn_c.CommandText = "select * from PJ"

Set Cnn_c.ActiveConnection = Cnn

Set Rs = Cnn_c.Execute

TreeView1.Nodes.Clear

If Rs.RecordCount > 0 Then

    For i = 1 To Rs.RecordCount

        TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")

        TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")

        Rs.MoveNext

    Next

    Rs.Close

End If

Cnn.Close

Set Cnn_c = Nothing

Set Rs = Nothing

Set Cnn = Nothing

End Sub

Private Sub DelRecord(ByVal id As String)

Dim i As Integer

Dim Cnn As ADODB.Connection

Dim Rs As ADODB.Recordset

Dim Cnn_c As New ADODB.Command

Set Cnn = New ADODB.Connection

Set Rs = New ADODB.Recordset

Cnn.ConnectionString = ConnStr

With Cnn

    .CursorLocation = adUseClient

    .Open

End With

Cnn_c.CommandType = adCmdText

Cnn_c.CommandText = "delete from PJ where BRAND='" & id & "'"

Set Cnn_c.ActiveConnection = Cnn

Set Rs = Cnn_c.Execute '执行删除

Cnn_c.CommandText = "select * from PJ"

Set Rs = Cnn_c.Execute '执行查询

TreeView1.Nodes.Clear

If Rs.RecordCount > 0 Then

    For i = 1 To Rs.RecordCount

        TreeView1.Nodes.Add , , "R" & i, Rs.Fields("BRAND")

        TreeView1.Nodes.Add "R" & i, tvwChild, , Rs.Fields("MODEL")

        Rs.MoveNext

    Next

    Rs.Close

End If

Cnn.Close

Set Cnn_c = Nothing

Set Rs = Nothing

Set Cnn = Nothing

End Sub

你方法思路是完全错误的。

一、假如你改的节点上的文本:

1、直接设置节点的文本为新值。

2、执行数据库更新 *** 作。

这样不用重新加载树。

二、假如你改的节点文本以外的数据:

1、修改数据

2、读取修改的数据,执行数据库更新 *** 作。

3、

点击一个节点,获取数据库中其他字段的数据:点节点后,执行数据库查询 *** 作,将查询结果传递给其他控件,如textbox。

这样,你根本不需要重新加载树,如果记录很多,仅仅加载树就是一个很长的过程。所以你的方法是不科学的。

三、添加节点、更改、删除节点也是如此。

总之,不管什么 *** 作,都不会重新加载树,状态不会改变。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6780194.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-28
下一篇2023-03-28

发表评论

登录后才能评论

评论列表(0条)

    保存