【IT168技术文档】LOTUS NOTES 为用户提供了强大的文档式数据库,在开发中利用NOTES 提供的各种对象,“表单”;“视图”能很好的组织,查找数据。但是开发中需要生成数据的动态表格,如VB中的DBGRID。笔者在开发中用SQL SERVER为后端数据库,或者动态的搜索NOTES数据库中的值生成动态表格。下面的例子为出勤统计,员工请假后将在SQL SERVER数据库中登记,每月进行统计。
方法如下:
一.建立与数据源的连接的准备
我的后端数据库为SQL SERVER,首先在ODBC中添加“系统DSN”按步骤把要添加的数据源添加到“系统DSN”中,如图中的“gwgl”,设置好以后准备在NOTES中进行连接。
二.NOTES 中与ODBC的连接
在NOTES 中共提供了三个对象用于操作数据库,
1.ODBCConnection用于连接到ODBC 数据源
2.ODBCQuery用于定义一个SQL 语句
3.ODBCREsultSet在结果集合上执行操作
用ODBCConnection对象的connectTo成员函数进行连接。详细语句请看例子。
三.NOTES 中对SQL数据库的操作
在NOTES对数据库进行操作主要使用ODBCQUERY和ODBCRESULTSET。其中使用ODBCQUERY 的sql 属性 能灵活的对数据库进行有效的管理。
此例的要求是这样的,在对公司的请销假的管理中,客户要求每月生成“考勤表”,而用户在请假和销假时都只是标出假期的开始和结束时间,生成一个全月每一天的记录难以实现。后端用sql 数据库,每当用户请假时插入一条记录,如已有此人的记录则更新记录。
在每月的月末再把其他未请假的职工生成默认的记录。
在访问ODBC类前必须在(Golbal)对象中输入以下语句:
uselsx “*lsxodbc”
在NOTES中创建请销假表单,并创建一个“操作”,名为“登记”,当用户完成请假流程后此操作将出现。以下编码首先读取请假人的姓名和请假的时间,判断是否由此记录,如有则更新记录,如没有则插入记录。其编码为:
Sub Click(Source As Button) Dim workspace As New NotesUIWorkspace Dim uidoc As NotesUIdocument. Set uidoc = workspace.Currentdocument. Dim con As New ODBCConnection Dim qry As New ODBCQuery Dim result As New ODBCResultSet Dim firstName As String Dim lastName As String Dim msg As String '声明数据库对象 Set qry.Connection = con Set result.Query = qry con.silentmode=True Dim ts As Integer If con.ConnectTo("gwgl","sa","") Then yf=uidoc.fieldgettext("dn") ….. nf=uidoc.fieldgettext("nf") '取得NOTES 表单中域的值 Dim odate As Variant sta1=Cint(Right(uidoc.fieldgettext ("xj_sta1"),2)) end1=Cint(Right(uidoc.fieldgettext ("xj_end1"),2) '取得假期的开始和结束时间 schstr="select qxj_xm from qxj where qxj_xm='" & xm &"' and year='" & nf & "' and month='" & yf & "'" qry.sql=schstr result.execute If result.IsResultSetAvailable Then ‘判断是否存在该数据 Dim n As Integer Const qz="qxj_" For i=sta1 To end1 ym=qz & i schstr="update qxj set " & ym &"='" & lx & "' where qxj_xm=" & "'" & xm & "'" n=n+1 Messagebox schstr qry.sql=schstr result.execute Next schstr="update qxj set " & lx &"='" & n & "' where qxj_xm=" & "'" & xm & "'" qry.sql=schstr result.execute p=ts-n schstr="update qxj set qxj_sc='" & p & "' where qxj_xm=" & "'" & xm & "'" qry.sql=schstr result.execute Else '存在该数据进行插入操作 Dim intstr As String intstr="insert qxj (qxj_xm,year,month,qxj_sc) values('" & xm & "','" & nf & "','" & yf & "','" & ts & "')" qry.sql=intstr result.execute Const qz1="qxj_" For i=sta1 To end1 ym=qz1 & i schstr="update qxj set " & ym &"='" & lx & "' where qxj_xm=" & "'" & xm & "' and year='" & nf & "' and month='" & yf & "'" n=n+1 qry.sql=schstr result.execute schstr="update qxj set " & lx &"='" & n & "' where qxj_xm=" & "'" & xm & "' and year='" & nf & "' and month='" & yf & "'" qry.sql=schstr result.execute p=ts-n schstr="update qxj set qxj_sc='" & p & "' where qxj_xm=" & "'" & xm & "' and year='" & nf & "' and month='" & yf & "'" qry.sql=schstr result.execute Next End If End If End Sub