從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")