⑷.Project>> References,引用COM+ Service Type Library和Microsoft Active Server Pages Object Library。
⑸.修改類代碼如下:
from www.survivalescaperooms.com
'建立數(shù)據(jù)庫連接并輸出數(shù)據(jù)庫字段 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是值得推薦的,因為它具有下列優(yōu)點: n 動態(tài)卸載平衡,提高組件和基于組件的應(yīng)用程序的升級性。 n 包含公布和提交事件和隊列組件的能力,使得更容易與多個組件聯(lián)合。
要想使組件具有MTS的特性,必須對組件做少許改動。在NT和98下開發(fā)時,必須在項目中引用Microsoft Transaction Server Type Library,在Windows 2000下開發(fā),必須引用COM+ Service Type Library。
asp_use_com.asp <% 'asp調(diào)用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發(fā)送電子郵件 ⑴.安裝Microsoft SMTP Service ⑵.設(shè)置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個與下列各條對應(yīng)的參數(shù)。 l 郵件發(fā)送者的email地址 l 郵件接收者的email地址 l 郵件主題 l 郵件內(nèi)容
使用方法: <% fromWho=… toWho=… Subject=… Body=…
IF toWho <> "" THEN sendMail fromWho, toWho, Subject, Body END IF %>
'定義獲得文件后綴的函數(shù) function getfileextname(filename) pos=instrrev(filename,".") if pos>0 then getfileextname=mid(filename,pos+1) else getfileextname="" end if end function
'定義獲取文件正名的函數(shù) function getfilename(filename) lens=len(filename)-len(getfileextname(filename))-1 getfilename=left(filename,lens) end function
'創(chuàng)建文件上傳組件的對象 set fileup=server.createobject("chinaasp.upload")
'循環(huán)讀取用戶上傳的文件,并保存在服務(wù)器上 for each f in fileup.files
'當(dāng)用戶沒有選擇文件或文件大小超過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 "發(fā)生錯誤,請確認文件是否存在" 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 "發(fā)生錯誤,請確認文件是否正確創(chuàng)建" 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.配置數(shù)據(jù)庫:數(shù)據(jù)庫名course,用戶course_user,密碼course_password,ODBC驅(qū)動為course_dsn,端口為2433,描述表結(jié)構(gòu)的腳本在共享目錄下。 3.Asp fileup、Jmail、Winzip 8.1、Winzip command line這幾個軟件請自行下載。 4.數(shù)據(jù)庫腳本文件: 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