用vfp建立3层客户服务器程序

来源:计算机等级考试    发布时间:2012-08-28    计算机等级考试视频    评论

  载入表单

  要调用 OLE 服务程序, 表单使用与调用 Excel 或 Word 相同的CreateObject() 函数. 出现在Windows 注册表中的 OLE 程序程序的正式的注册名是 vfpLibraryOLEServer.Members. 以下是表单的 Load 事件中的代码.

  THIS.olibrary = CREATEOBJECT("vfpLibraryOLEServer.Members")

  IF TYPE("This.oLibrary") # "O" OR ISNULL(THIS.olibrary)

  = MESSAGEBOX("Couldn't set up the OLE Server.", mb_iconinformation)

  THIS.lconnected = .F.

  RETURN

  ELSE

  IF THIS.olibrary.initconnection("robertg","vfpguru")

  = MESSAGEBOX("Welcome to the library", mb_iconinformation)

  THISFORM.lconnected = .T.

  ELSE

  = MESSAGEBOX("Access to the library denied", mb_iconinformation)

  THIS.lconnected = .F.

  RETURN

  ENDIF

  ENDIF

  这里的代码试着创建一个 OLE 服务程序的引用. 如果成功, 表单属性 oLibrary 将包含一个 OLE 服务程序的引用. 如果引用不能创建则剩下的事不会继续. 如果对象可以创建, 表单将调用 OLE 服务程序的 InitConnection 方法来试图登录到 SQL Server. 如果失败, 则什么也不做.

  如果连接成功, 表单创建一个游标然后设置开放式行缓存. 游标将用于添加和修改, 并允许你使用你熟悉的数据输入函数如 GetFldState() 和 OldVal(), 你将在稍后看到.

  SET MULTILOCKS ON

  CREATE CURSOR c_member ;

  ( member_no i, firstname c(50), middleinitial c(1), ;

  lastname c(50), street c(50), city c(50), state c(10), ;

  zip c(10), phone_no c(20), expr_date T, ;

  birth_date T, adult_member_no i )

  = CURSORSETPROP("Buffering", db_bufoptrecord, 'c_member')

  连接到 SQL SERVER

  OLE 的 InitConnection 方法用用户 ID 和口令作为参数然后用 SQLConnect() 来连接到 SQL Server. 如果连接成功该方法返回 .T., 否则返回 .F..

  lParameters cUserID, cPassword

  This.nHandle = SQLConnect('Library', cUserID, cPassword)

  Return Iif(This.nHandle < 0, .F., .T.)

  定位成员

  OLE 服务程序的 GetMember 方法用于获取一个成员信息. 在《整合 SQL Server 和 Visual FoxPro》一章中的 SQL pass-through 示例中的示例表单使用 SQLExec() 来运行一条 Select 语句或执行一个储存过程. 两种情况下结果将作为一个游标返回, 该游标将用于表单中的数据操作.

  当使用特别用于客户的 OLE 服务程序时(包括 Access 和 Visual Basic), 生活不是这么简单了. 因此它不会简单地在一个 Visual FoxPro 游标中返回结果.

  OLE 服务程序的 GetMember 方法接受三个参数: 一个需要获取的数据的列表的串, 你要用作为分隔符的字符和你想要获取的成员的 ID . 表单的 Locate 按钮的 Click 事件调用 OLE 服务程序的 GetMember 方法.

  该串是按引用传递, 因为 GetMember 将用成员信息填充它. 分隔符字符将用于分隔信息中的字段. 如果 GetMember 返回 0, 没有所提供的 ID 成员. 如果它返回一个负数, 则发生了错误, 该错误信息保存在 OLE 服务程序的 LastErrDesc 属性中.

  nretval = THISFORM.olibrary.getmember(@lcmemberinfo, "|", ;

  THISFORM.txtmemberid.value)

  IF nretval < 0

  lcmessage = THISFORM.olibrary.lasterrdesc

  = MESSAGEBOX(SUBSTR(lcmessage, RAT(']',lcmessage)+1), ;

  mb_iconinformation)

  <省略的代码>

  IF nretval = 0

  = MESSAGEBOX("There is no member with this ID.", ;

  mb_iconinformation)

  <省略的代码>

  如果找到了成员信息, 就需要分解中来读取信息中的各个字段值. 表单的 Load 方法中创建的游标用这些信息填充且表单显示被刷新.

  FOR i = 1 TO 10

  j = ALLTRIM(STR(i))

  npipe&j = AT("|", lcmemberinfo, i)

  NEXT

  npipe11 = LEN(lcmemberinfo)

  SELECT c_member

  APPEND BLANK

  REPLACE firstname WITH SUBSTR(lcmemberinfo, 1, npipe1 - 1), ;

  middleinitial WITH SUBSTR(lcmemberinfo, npipe1 + 1, ;

  npipe2 - npipe1 - 1), ;

  lastname WITH SUBSTR(lcmemberinfo, npipe2 + 1, ;

  npipe3 - npipe2 - 1), ;

  street WITH SUBSTR(lcmemberinfo, npipe3 + 1, ;

  npipe4 - npipe3 - 1), ;

  city WITH SUBSTR(lcmemberinfo, npipe4 + 1, ;

  npipe5 - npipe4 - 1), ;

  state WITH SUBSTR(lcmemberinfo, npipe5 + 1, ;

  npipe6 - npipe5 - 1), ;

  zip WITH SUBSTR(lcmemberinfo, npipe6 + 1, ;

  npipe7 - npipe6 - 1), ;

  phone_no WITH SUBSTR(lcmemberinfo, npipe7 + 1, ;

  npipe8 - npipe7 - 1), ;

  expr_date WITH CTOT(SUBSTR(lcmemberinfo, npipe8 + 1, ;

  npipe9 - npipe8 - 1)), ;

  birth_date WITH CTOT(SUBSTR(lcmemberinfo, npipe9 + 1, ;

  npipe10 - npipe9 - 1)), ;

  adult_member_no WITH VAL(SUBSTR(lcmemberinfo, npipe10 + 1, ;

  npipe11 - npipe10 - 1))

  <省略的代码>

  THISFORM.REFRESH

  <省略的代码>

  在 For loop 中的代码检查各管道分隔符的位置. 第一个分隔符前的是该成员的 first name. 第一个分隔符和第二个分隔符间的东西是成员姓名的中间大写字母. 第一个分隔符和第二个分隔符间的东西是成员的 last name. 等等.

  OLE 服务程序的GetMember 方法

  正如上面所提及, OLE 服务程序的 GetMember 方法接受三个参数, 一个包含成员信息的分隔符分隔的列表, 分隔符和所需成员的成员 ID. 构造一个 Select 语句并发送到 SQL Server 来获取信息. 如果找到成员, 串被创建. 由于是按引用传递它将被发送回表单. 如果成员没有找到 GetMember 方法自己返回 0 , 否则返回 1.

  LPARAMETERS cmemberinfo, cdelimiter, csearchid

  lcsql = "Select firstname, middleinitial, lastname, street, " + ;

  " city, state, zip, phone_no, expr_date, " + ;

  " birth_date = null, adult_member_no = null " + ;

  "from member, adult " + ;

  "where member.member_no = adult.member_no " + ;

  " and member.member_no = " + ALLTRIM(csearchid) + " " + ;

  "union " + ;

  "select firstname, middleinitial, lastname, street, " + ;

  " city, state, zip, phone_no, expr_date, " + ;

  " birth_date, adult_member_no " + ;

  "from member, adult, juvenile " + ;

  "where member.member_no = juvenile.member_no " + ;

  " and adult.member_no = juvenile.adult_member_no " + ;

  " and member.member_no = " + ALLTRIM(csearchid)

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答