⑷.Project>> References,引用COM+ Service Type Library和Microsoft Active Server Pages Object Library。
⑸.修改类代码如下:
from www.VeVb.com
'建立数据库连接并输出数据库字段 Dim Response As Response Dim Request As Request Dim Server As Server Dim application As Application Dim session As Session
Private Sub Class_Initialize() Dim objContext As ObjectContext Set objContext = GetObjectContext() Set Response = objContext("Response") Set Request = objContext("Request") Set Server = objContext("Server") Set Application = objContext("Application") Set Session = objContext("Session") End Sub
Sub conn_db() Set conn = CreateObject("adodb.connection") conn.open "course_dsn", "course_user", "course_passWord" Set rs = CreateObject("adodb.recordset") rs.open "select * from user_info", conn, 1, 1
If rs.recordcount > 0 Then For i = 1 To rs.recordcount Response.write "<br>" & rs("user_name") & "<br>" If rs.EOF Then Exit For rs.movenext Next End If rs.Close Set rs = Nothing conn.Close Set conn = Nothing End Sub
⑹.添加一新类cutstr
⑺.修改类代码如下:
'截取字符串 Function cutstr(str, length) If Len(str) > length Then cutstr = Left(str, length) & "..." Else cutstr = str End If End Function
⑻.File>>Save
⑼.File>>make course.dll
3. 注册组件:MTS和regsvr32.exe
有两种方式注册组件:MTS和使用regsvr32.exe。MTS是值得推荐的,因为它具有下列优点: n 动态卸载平衡,提高组件和基于组件的应用程序的升级性。 n 包含公布和提交事件和队列组件的能力,使得更容易与多个组件联合。
要想使组件具有MTS的特性,必须对组件做少许改动。在NT和98下开发时,必须在项目中引用Microsoft Transaction Server Type Library,在Windows 2000下开发,必须引用COM+ Service Type Library。
asp_use_com.asp <% 'asp调用com组件 set cutstr_obj=server.createobject("course.cutstr") response.write cutstr_obj.cutstr("abcdefghijk",3)&"<br>" set cutstr_obj=nothing
set conn_obj=server.createobject("course.conn_db") conn_obj.conn_db() set conn_obj=nothing %>
使用Microsoft Smtp发送电子邮件 ⑴.安装Microsoft SMTP Service ⑵.设置Microsoft SMTP Service ⑶.代码部分: mail_smtp.asp <% sub sendmail(fromwho,towho,subject,body) dim mymail set mymail = server.createobject("cdonts.newmail") mymail.from = fromwho mymail.to = towho mymail.subject = subject mymail.body = body mymail.send set mymail = nothing end sub %>
该子程序接受4个与下列各条对应的参数。 l 邮件发送者的email地址 l 邮件接收者的email地址 l 邮件主题 l 邮件内容
使用方法: <% fromWho=… toWho=… Subject=… Body=…
IF toWho <> "" THEN sendMail fromWho, toWho, Subject, Body END IF %>
'定义获得文件后缀的函数 function getfileextname(filename) pos=instrrev(filename,".") if pos>0 then getfileextname=mid(filename,pos+1) else getfileextname="" end if end function
'定义获取文件正名的函数 function getfilename(filename) lens=len(filename)-len(getfileextname(filename))-1 getfilename=left(filename,lens) end function
'创建文件上传组件的对象 set fileup=server.createobject("chinaasp.upload")
'循环读取用户上传的文件,并保存在服务器上 for each f in fileup.files
'当用户没有选择文件或文件大小超过10m时返回到选择上传文件的页面 if f.filename="" or f.filesize>10485500 then response.redirect "upload_file.htm"
Sub instead(word) Set myRange = word.ActiveDocument.Content for i=0 to Var_Num - 1 call myRange.Find.Execute(varStrings(i),false,false,false,false,false,false,false,false,varValues(i),2) Next End Sub
w1="word.activedocument.saveAs"&chr(32)&chr(34)&filenames&chr(34) w2="wApp.Documents.open"&chr(32)&chr(34)&filenames&chr(34) %> <script language="vbscript"> On Error Resume Next '生成指定文件名的Word文档 Dim word set word = CreateObject("Word.Application") if Err.number > 0 Then Alert "发生错误,请确认文件是否存在" else word.visible = False word.documents.open "<%response.write path%>course.dot" <%Response.write w1%> word.documents.close set word=nothing end if
<!--#include file="opr_doc_inc.asp"-->
Dim wApp Set wApp = CreateObject("Word.Application") If Err.number > 0 Then Alert "发生错误,请确认文件是否正确创建" else wApp.visible = True <%Response.write w2%> call instead(wApp) set wApp=nothing end if </script>
附: 1.以上全部代码在Windows 2000 Server SP2+IIS 5.0+MS SQL Server 2000+Office 2000下测试通过 2.配置数据库:数据库名course,用户course_user,密码course_password,ODBC驱动为course_dsn,端口为2433,描述表结构的脚本在共享目录下。 3.Asp fileup、Jmail、Winzip 8.1、Winzip command line这几个软件请自行下载。 4.数据库脚本文件: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[output_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[output_1] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[return_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[return_1] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[user_info_1] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[user_info_2] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_3]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[user_info_3] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[user_info] GO
CREATE TABLE [dbo].[user_info] ( [id] [int] IDENTITY (1, 1) NOT NULL , [user_name] [varchar] (40) COLLATE Chinese_PRC_CI_AS NOT NULL , [password] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ) ON [PRIMARY] GO
ALTER TABLE [dbo].[user_info] WITH NOCHECK ADD CONSTRAINT [PK_user_info] PRIMARY KEY CLUSTERED ( [user_name] ) ON [PRIMARY] GO
SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO
CREATE PROCEDURE [output_1] @sid int output AS set @sid=2 GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO
CREATE PROCEDURE [return_1] (@user_name varchar(40),@password varchar(20)) AS if exists(select id from user_info where user_name=@user_name and password=@password) return 1 else return 0 GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS OFF GO
CREATE PROCEDURE [user_info_1] (@user_name varchar(40),@password varchar(20)) AS select id from user_info where user_name=@user_name and password=@password GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO
CREATE PROCEDURE [user_info_2] (@user_name varchar(40),@password varchar(20)) AS SET XACT_ABORT ON BEGIN TRANSACTION delete from user_info where user_name=@user_name and password=@password COMMIT TRANSACTION SET XACT_ABORT OFF GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO
CREATE PROCEDURE [user_info_3] AS select * from user_info GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO