數(shù)據(jù)庫連接參數(shù)設(shè)置
'*****定義數(shù)據(jù)庫連接參數(shù) Dim rs As New ADODB.Connection Dim bs As New ADODB.Recordset Dim sql As String '查詢字符串 Dim filename As String '數(shù)據(jù)庫名稱 Dim ctrFi '連接字符串 Dim nodX As Node '樹形控件節(jié)點(diǎn)類型定義
連接并打開數(shù)據(jù)庫:
'******連接并打開數(shù)據(jù)庫********** Public Sub connectdata() filename = App.Path + "\" + "123.mdb" ctrFi = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & filename rs.Open ctrFi End Sub
數(shù)據(jù)庫中表與字段設(shè)置:
數(shù)據(jù)庫名稱:123.mdb
表一:
名稱:usere
字段:user(字符型),type(字符型)
表二:
名稱:typer
字段:usertype(字符型)
窗口上加入一個(gè)Imagelist控件,加入3個(gè)圖標(biāo),再加入一個(gè)treeview控件,treeview控件圖標(biāo)屬性與imagelis關(guān)聯(lián)(即在treeview控件上點(diǎn)右鍵,選擇圖像列表里的imagelist1,必須先添加imagelist1控件才有顯示)
添加節(jié)點(diǎn)代碼:
TreeView1.LineStyle = tvwRootLines Call connectdata sql = "select * from typer" '添加根節(jié)點(diǎn) bs.Open sql, rs, 1 If bs.RecordCount <> 0 Then bs.MoveFirst Do While bs.EOF = False Set nodX = TreeView1.Nodes.Add(, , bs.Fields("usertype").Value, bs.Fields("usertype").Value, 3) bs.MoveNext Loop End If bs.Close rs.Close '***添加子節(jié)點(diǎn)*** Call connectdata sql = "select * from usere" bs.Open sql, rs, 1 If bs.RecordCount <> 0 Then bs.MoveFirst Do While bs.EOF = False Set nodX = TreeView1.Nodes.Add(bs.Fields("type").Value, tvwChild, bs.Fields("user").Value, bs.Fields("user").Value, 2) bs.MoveNext Loop End If bs.Close rs.Close
|