1.数据绑定
Private Sub mnuLinkdata_Click()
'*************************连接数据库*******************
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = "driver=sql server;server=127.0.0.1;uid=sa;pwd=jsly;database=jsly"
conn.Open
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandText = "select * from tunnel"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set rs = cmd.Execute
'*************************连接数据库*******************
'****************************绑定数据*******************
Dim ds As Dataset
Set ds = Map1.DataSets.Add(miDataSetADO, rs, "ds", "ID", , "点管道")
'DataSets.Add Type, SourceData, [Name], [Geofield], [SecondaryGeofield], [BindLayer], [Fields], [Dynamic]
MsgBox "绑定成功!"
'****************************绑定数据*******************
mnuLinkdata.Enabled = False
End Sub
2.获取图元对应数据库信息
Private Sub Map1_DblClick()
'2、获取图元信息
Dim Layer As MapXLib.Layer
Dim ds As MapXLib.Dataset
Dim fields As MapXLib.fields
Dim ftr As MapXLib.Feature
Set ds = Map1.DataSets("ds")
Set Layer = ds.Layer
Set fields = ds.fields
For Each ftr In Layer.Selection
'MsgBox Layer.AllFeatures.Item(ftr.FeatureKey).Name
frmShowData.Label1.Caption = fields.Item(2) & ": " & ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(2))
frmShowData.Label2.Caption = fields.Item(3) & ": " & ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(3))
frmShowData.Label3.Caption = fields.Item(4) & ": " & ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(4))
frmShowData.Label4.Caption = fields.Item(5) & ": " & ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(5))
frmShowData.Label5.Caption = fields.Item(6) & ": " & ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(6))
Next
frmShowData.Show
End Sub
3.开始数据监测,数据库表的第二个字段大于5的图元style变化
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim Layer As MapXLib.Layer
Dim ds As MapXLib.Dataset
Dim fields As MapXLib.fields
Dim ftr As MapXLib.Feature
'Dim Oldsytle As MapXLib.Style
Set ds = Map1.DataSets("ds")
Set Layer = ds.Layer
Set fields = ds.fields
For Each ftr In Layer.AllFeatures
'MsgBox Layer.AllFeatures.Item(ftr.FeatureKey).Name 用来确定图元(行)
'MsgBox fields.Item(2) 用来确定字段(列)
If ds.Value(Layer.AllFeatures.Item(ftr.FeatureKey).Name, fields.Item(2)) > 5 And ftr.Style.SymbolBitmapSize = 24 Then
ftr.Style.SymbolBitmapSize = 30
ftr.Update
Else
ftr.Style.SymbolBitmapSize = 24
ftr.Update
End If
Next
End Sub