从Ado中取出数据,返回时是一个RecordSet这里不能直接将RecordSet绑定到Grid中,因
为Grid,RecordSource不接收此种类型的数据结构,会产生个错误
正确的方法是做入一个游标中,然后将游标中绑定到Grid中
注意在使用从Procedure中返回的游标最好不要直接绑定到Grid
不然的话,数据会一闪而过,现在电脑运行速度快,是看不到的,测试的办法是在绑定后,加上一个MessageBox
比较简单的办法是使用一个全局变量来存放游标
写在按钮的ClickProcedure中的内容
SET PROCEDURE TO "E:\FoxProProject\sqlconnection.prg"
mycon=sqlConnection ("Provider=SQLOLEDB.1;Password=cxy;Persist Security Info=True;User ID=sa;Initial Catalog=bussetup;Data Source=.")
&&构造数据库命令
comString="select top 10000 * from tables "
PUBLIC mytable as CursorAdapter
mytable= executeNonQuery(mycon,comString)
&&MESSAGEBOX(mytable)
&&MESSAGEBOX(mytable.Fields(0).Value)
thisform.grid1.RecordSourceType=1
thisform.grid1.RecordSource="Customers"
thisform.grid1.Refresh()
thisform.Refresh()
MESSAGEBOX("done")
SqlConnection.Prg中的代码
&&使用Ado的方式来连接Sql Server
procedure SqlConnection
PARAMETERS cString
LOCAL oConn AS ADODB.Connection , oRs AS ADODB.RecordSet
oConn=CREATEOBJECT("ADODB.Connection")
oConn.Open(cString)
RETURN oConn
End proc
procedure ExecuteNonQuery
PARAMETERS oConn ,comStr
LOCAL oRs AS ADODB.RecordSet,;
loCursor AS CURSORADAPTER
oRs= oConn.Execute(comStr)
loCursor = CREATEOBJECT('CursorAdapter')
WITH loCursor
.ALIAS = 'Customers'
.DATASOURCETYPE = 'ADO'
llReturn = .CURSORFILL(.F., .F., 0, oRs)
ENDWITH
RETURN loCursor
&& RETURN oRs
&&RETURN oRs.Fields(0).Value
END proc
如何将一个表绑定到Grid中
SET PATH TO "E:\FoxProProject\data\"
CLOSE DATABASES
OPEN DATABASE gsdata
USE gsdata!xl SHARED
browse
thisform.grid1.RecordSourceType= 0
thisform.grid1.RecordSource="xl"
thisform.grid1.Refresh()
thisform.Refresh()
MESSAGEBOX("done")