VF中,我们可以通过ADO来取得数据并且可以绑定到窗体上
但是ADO使用使用起来并不是那么方便,比如不能方便地插入,更新数据,而在VF的本地环境中,如果加载了数据环境
那么这一切就变得比较轻松了,
这里我们可以做使用VF自带的数据环境中的ADO的自动生成CursorAdapter
的向导来,为我们生成相关的代码,然后加以改造就可以了
生成的SCX文件用EditPlus或者UtlraEdit打开就可以了,可以从里边清楚地看到代码
以下是我改造的一段代码
Define Class Data1 As DataEnvironment && 数据环境类
Name = "Dataenvironment"
Top = 220
Left = 1
Width = 520
Height = 200
Datasource = .Null.
DataSourceType = "ADO"
Procedure BeforeOpenTables
*** Select connection code: DO NOT REMOVE
Local loConnDataSource
loConnDataSource = Createobject('ADODB.Connection')
***<DataSource>
&&loConnDataSource.ConnectionString = [Provider=SQLOLEDB.1;Password=cxy;Persist Security Info=True;User ID=sa;Ini] + ;
&& [tial Catalog=bussetup;Data Source=.;]
loConnDataSource.ConnectionString = conString
***</DataSource>
loConnDataSource.Open()
This.Datasource = Createobject('ADODB.RecordSet')
This.Datasource.CursorLocation = 3 && adUseClient
This.Datasource.LockType = 3 && adLockOptimistic
This.Datasource.ActiveConnection = loConnDataSource
*** End of Select connection code: DO NOT REMOVE
ENDPROC
Enddefine && 添加 Cursor 类对象
DEFINE CLASS demo as CursorAdapter
UseDeDataSource= .T.
SelectCmd="select * from ........."
CursorSchema ="ID I, 日期 T"
Alias = "demotable"
Flags = 0
KeyFieldList = "ID"
Tables = ".......D"
UpdatableFieldList = "................"
UpdateNameList = "......................."
UseCursorSchema = .T.
Name = "demo"
Procedure Init
*** Setup code: DO NOT REMOVE
Local llReturn
Do Case
Case Not Pemstatus(This, '__VFPSetup', 5)
This.AddProperty('__VFPSetup', 0)
Case This.__VFPSetup = 1
This.__VFPSetup = 2
Case This.__VFPSetup = 2
This.__VFPSetup = 0
Return
Endcase
Set Multilocks On
llReturn = DoDefault()
*** End of Setup code: DO NOT REMOVE
*** Setup code: DO NOT REMOVE
If This.__VFPSetup = 1
This.__VFPSetup = 2
Endif
Return llReturn
*** End of Setup code: DO NOT REMOVE
Endproc
Procedure AutoOpen
*** Setup code: DO NOT REMOVE
If Not Pemstatus(This, '__VFPSetup', 5)
This.AddProperty('__VFPSetup', 1)
This.Init()
Endif
*** End of Setup code: DO NOT REMOVE
Endproc
ENDDEFINE
&&这是一些表格
DEFINE CLASS grdDemotable as Grid
columnCount = 29
Left = 48
RecordSource = "demotable"
RecordSourceType = 1
Top = 24
Name = "grdDemotable"
Column1.ControlSource = "demotable.id"
Column1.Name = "Column1"
................
...............
ENDDEFINE
Define Class cxyform As Form
Top = 0
Left = 0
Height = 386
Width = 587
DoCreate = .T.
Caption = "cxyform"
Name = "FORM1"
DataEnvironment="" && DataEnvironment 是表单的一个属性
Procedure Destroy
Try
Thisform.DataEnvironment.CloseTables && 在 destroy 过程的结束处关闭表
Catch To mException
RETURN .f.
ENDTRY
thisform.Hide
Clear Events
ENDPROC
Procedure Unload
Clear Events
ENDPROC
Procedure Load
* DataEnvironment 被实例化,且表单的属性
* DataEnvironment 引用该对象. DataEnvironment
* 并没有实际地添加到表单. THISFORM.DATAENVIRONMENT
* 只是一个指向 DataEnvironment 对象的指针.
myde=Createobject("Data1")
Thisform.DataEnvironment=myde
myde.ADDOBJECT("demoname", "demo")
&&myde.demoname.selectcmd="select top 10 * from ................"
myde.demoname.selectcmd=comSql
* 在 LOAD 开始时打开表.
Thisform.DataEnvironment.OpenTables
thisform.AddObject("bb","grdDemotable")
thisform.bb.visible=.T.
thisform.bb.Width=thisform.Width
thisform.bb.Height=thisform.Height
thisform.bb.Top=0
thisform.bb.Left=0
thisform.bb.Column1.visible=.f.
thisform.bb.Column2.visible=.f.
thisform.bb.Column3.visible=.f.
thisform.bb.Column4.visible=.f.
thisform.bb.Column5.visible=.f.
thisform.bb.Column6.visible=.f.
thisform.bb.Column7.visible=.f.
thisform.bb.Column8.header1.caption="站点名称"
thisform.bb.Column9.header1.caption="全票"
thisform.bb.Column12.visible=.f.
thisform.bb.Column13.header1.caption="结进金额"
BINDEVENT(this,"Resize",this,"changesize")
ENDPROC
PROCEDURE changesize
thisform.bb.width=thisform.Width
thisform.bb.height=thisform.Height
ENDPROC
Enddefine