深入CursorAdapter(三)
来源:优易学  2011-12-9 18:16:49   【优易学:中国教育考试门户网】   资料下载   IT书店

 

 还有一些方法这里我们就不说了,你可以自己去研究它们。HandleError 方法使用 Aerror() 来判断发生了什么错误,并把错误数组的第二个元素放到 cErrorMessage 属性中去。Requery 方法与 GetData 类似,不过它是用来刷新 Cursor 中的数据。调用这个方法而不是 CursorRefresh 方法的原因就象 GetData 一样:它能够处理参数和错误。Update 方法很简单:它就是调用 TableUpdate() 来提交当前数据源的更新,如果提交更新失败,则调用 HandleError 方法来处理错误。AddTag 用于在 Cursor 被建立后将你想要建立的索引的信息添加到 Tags 集合中,而 GetData 方法会调用的 CreateTags 方法则会在自己的 Index ON 语句中用到这个集合中的信息。

  这里是使用这个类的一个例子,是从附件中的 TestCursorAdapter.prg 中拿来的。它从 SQL Server 自带的 Northwind 数据库的 Customers 表中取得数据。它的 SelectCmd 属性里是一个参数化查询的 Select 语句,向你演示了怎样用 AddParameter 方法来处理参数,以及怎样用 AddTag 方法来自动地为 Cursor 建立索引标识。

  local loCursor as SFCursorAdapter of SFCursorAdapter, ;
  loConnMgr as SFConnectionMgrODBC of SFRemote, ;
  loParameter as Empty
  lnHandle = sqlstringconnect('driver=SQL Server;server=(local);' + ;
  'database=Northwind;uid=sa;pwd=;trusted_connection=no')
  && change password to appropriate value for your database
  loCursor = newobject('SFCursorAdapter', 'SFDataClasses')
  with loCursor
  .DataSourceType = 'ODBC'
  .Alias = 'Customers'
  .SelectCmd = 'select * from customers where country = ?pcountry'
  .SetConnection(lnHandle)
  .AddParameter('pcountry', 'Brazil')
  .AddTag('CustomerID', 'CustomerID')
  .AddTag('Company', 'upper(CompanyName)')
  .AddTag('Contact', 'upper(ContactName)')
  if .GetData()
  messagebox('Brazilian customers in CustomerID order')
  set order to CustomerID
  go top
  browse
  messagebox('Brazilian customers in Contact order')
  set order to Contact
  go top
  browse
  messagebox('Canadian customers')
  loParameter = .GetParameter('pcountry')
  loParameter.value = 'Canada'
  .Requery()
  browse
  else
  messagebox('Could not get the data. The error message was:' + ;
  chr(13) + chr(13) + .cErrorMessage)
  endif .GetData()

  * Now try to do an invalid SELECT statement.

  .SelectCmd = 'select * from customersx'
  if .GetData()
  browse
  else
  messagebox('Could not get the data. The error message was:' + ;
  chr(13) + chr(13) + .cErrorMessage)
  endif .GetData()
  endwith
  sqldisconnect(lnHandle)

  SFDataEnvironment

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训