Teach you to use VB to create ASP components

Source: Internet
Author: User
Tags contains delete key implement object model
Create One, what is the component

A component is an object that contains code in the form of data that can be changed, and is an encapsulation of available code that can be used to perform some of the functionality of an application, such as retrieving certain information from a database. Now often heard about COM, which means that the Component Object model (Component object models), is developed by Microsoft in the Windows platform to execute the object model, COM defines the interaction between components. Components written for COM can be reused and do not depend on the language in which it is written, for example, a component can be written in vb,vc++ or even Java, as long as the COM specification is adhered to.

   Second, why we need to use VB to create ASP components

As I said earlier, writing components is not limited to the language you use, so you can write components using the language you are familiar with. We say that VB is used to write components The easiest language tool, although it does not have VC + + and Java excellent performance, and high-speed flexibility, but because it easy to learn to use, so the vast number of program developers are used to write COM components. Next I want to explain how to use VB to create the COM components used by ASP.

   third, the question of the proposed

A business web site often has to add or delete more CDs information than they provide, so they develop a DNA component (Distributed internet Architecture Distributed internet architecture) to manipulate CD information from the database. Here, we want to use VB to implement the process of creating this component.

First, we need to know the CD information in the database, such as the storage structure in SQL SERVER 7.0, as follows:

CD_ID: An identifier that uniquely identifies this CD in the database.

Cd_name: for CD name

Cd_author: For Singing (playing)

Cd_price: for CD price

Cd_information: A simple description of the CD

For the sake of simplicity, we assume that the processing of CD information data is only increased and deleted, interested friends want to increase the functionality of this component, please refer to the corresponding books, the completion of their own.

Like writing JavaBean programs in Java, in the ASP's component, we also need to define the corresponding methods to implement certain functions, in this example, the following methods are used:

OPENDB: Open the database and read all the CD information.

Closedb: Closes the database connection.

Add: Adds a new CD information to the database

Delete: Deletes a CD information from the database.

NEXTCD: Returns the current CD information from the table and moves the pointer to the next message

IsEof: To determine if there is more CD information

   Iv. using VB to create components

Now let's start by creating a new ActiveX DLL project with Visual Basic 6.0 (which, of course, is also possible with VB5.0) (figure)

Since we want to manipulate the database to use ADO, we will refer to the Microsoft Activex Data Object 2.5 Library. (pictured below)

Now we begin to write the method in the component

1, Opendb method

The idea of the Opendb method is to establish a connection with the MS SQL SERVER7.0, which can be implemented using SQLOLEDB provider. Once you have established a connection, you can create a recordset that contains all the CD information. Based on the above analysis, we write the following code.

Option Explicit

Private Conn as ADODB. Connection

Private Recordsetcd as ADODB. Recordset

' Open connection and result set

Private Sub opendb ()

Set Conn = New ADODB. Connection

Set RECORDSETCD = New ADODB. Recordset

Conn.Open "Provider=sqloledb;datasource=muse;initial Catalog=music; Userid=wayne; PASSWORD=ABCDEFG "

Recordsetcd.open "SELECT * from CD", Conn

End Sub

2, Closedb method

Since connecting to a database is quite expensive, it is important to remember to close the Recordset and connection objects when you do not need to use the database, closedb to do so.

Private Sub Closedb ()

' Close result set RECORDSETCD

If not (Recordsetcd are nothing) Then

If recordsetcd.state = adStateOpen Then

Recordsetcd.close

End If

Set Recordsetcd = Nothing

End If

' Close Connection conn

If not (Conn are nothing) Then

Conn.close

Set Conn = Nothing

End If

End Sub

3, Add method

The purpose of the Add method is to add a new CD information to the database, so the recordset it opens requires that it be updated. This does not open the dataset with open only, as in Opendb. See below for detailed code:

Private Sub Add (Id As String, Name as String, Author as String, price as Currency, information as String)

Dim Newconn as New ADODB. Connection

Dim Newrecordsetcd as New ADODB. Recordset

Newconn.open "Provider=sqloledb;datasource=muse;initial Catalog=music; Userid=wayne; PASSWORD=ABCDEFG "

Newrecordsetcd.open "SELECT * from CD", Newconn, adOpenDynamic, adLockOptimistic

Newrecordsetcd.addnew

Newrecordsetcd.fields ("cd_id") = ID

Newrecordsetcd.fields ("cd_name") = Name

Newrecordsetcd.fields ("cd_author") = Author

Newrecordsetcd.fields ("cd_price") = Price

Newrecordsetcd.fields ("cd_information") = Information

Newrecordsetcd.update

End Sub

Explanation: Using Newrecordsetcd.addnew,

Newrecordsetcd.fields ("Some_key") =some_value

Newrecordsetcd.update to add a record

4. Delete method:

The purpose of this method is to delete a CD name and related information in the library. It is implemented by locating the CD information through the unique cd_id in the query table, thereby deleting the row record

Private Sub Delete (Id as String)

Dim Newconn as New ADODB. Connection

Dim Recordsetdelete as New ADODB. Recordset

Newconn.open "Provider=sqloledb;datasource=muse;initial Catalog=music; Userid=wayne; PASSWORD=ABCDEFG Delete a piece of information from a table

Newconn.execute "DELETE from CD WHERE cd_id = '" & ID & "", adCmdText + adExecuteNoRecords

End Sub


5, the preparation of ASP program

(1) listcdinformation.asp procedure

This program is to retrieve the database, display the results of the search as a table, using the components we have just defined. It opens the table, and then lists the information for each CD.

<%

Dim Cd_info

Set cd_info= Server.CreateObject ("Cd.") Cd_handle ")

%>

<HTML>

<HEAD>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

&LT;TITLE&GT;CD Information List </TITLE>

</HEAD>

<BODY>

<b><p align= "Center" &GT;CD Information list </P></B>

<table border= "2" cellspacing= "1" >

<TR>

<td><p align= "center" &GT;CD number </TD>

<td><p align= "center" &GT;CD name </TD>

<td><p align= "center" > Author </TD>

<td><p align= "center" > Price </TD>

<td><p align= "center" &GT;CD profile </TD>

</TR>

<TR></TR>

<%dim ID

Dim Name

Dim Author

Dim Price

Dim Information

' Initialize CD list

Cd_info.opendb

While Cd_info.iseof =false

Cd_info.nextcd id,name,author,price,information

%>

<TR>

<TD><%=ID%></TD>

<TD><%=Name%></TD>

<TD><%=Author%></TD>

<TD><%=Price%></TD>

<TD><%=Information%></TD>

<td><form Name = "Delete" method = ' POST ' ACTION = ' deletecdinformation.asp?id=<%=id%> ' >

<input TYPE "SUBMIT" values= "Delete this bar" name= "DELETE2" >

</FORM>

</TD>

</TR>

<%

Wend

%>

</TABEL>

<form name= "ADD" method = "POST" action= "addcdinformation.asp" >

<input type = "Submit" value= "Add a CD info" name= "ADD2" >

</FORM>

</body>

(2) Deletecdinformation.asp Program

Clicking the Delete key after each piece of information deletes the message from the table, and it also calls the component we just registered. Limited to space, I write only the most important lines of code:

<%

Dim Cd_info

Set cd_info =server.createobject ("CD.") Cd_handle ")

Cd_info.delete request.querystring ("ID")

%>

(3) Addcdinformation.html

This program is a pure HTML text, only appear a form, let you fill out new CD information, and then sent to insertintodb.asp to join the database.



<meta http-equiv= "Content-language" content= "ZH-CN" >

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<title> Please add a CD information </title>


<body>

<form method= "POST" action= "insertintodb.asp" >

<p> </p>

<p> Please add a CD information </p>

<table border= "1" cellspacing= "1" width= "34%" height= "109" >

<tr>

&LT;TD width= "24%" height= ">"

<p align= "center" &GT;CD </td>

&LT;TD width= "76%" height= "><input type=" text "name=" ID "size=" ></td>

</tr>

<tr>

&LT;TD width= "24%" height= ">"

<p align= "center" &GT;CD name </td>

&LT;TD width= "76%" height= "><input type=" text "name=" name "size=" ></td>

</tr>

<tr>

&LT;TD width= "24%" height= ">"

<p align= "center" > Author </td>

&LT;TD width= "76%" height= "><input type=" text "name=" Author "size=" ></td>

</tr>

<tr>

&LT;TD width= "24%" height= ">"

<p align= "center" > Price </td>

&LT;TD width= "76%" height= "><input type=" text "name=" price "size=" ></td> "

</tr>

<tr>

&LT;TD width= "24%" height= ">"

<p align= "Center" > Introduction </p>

<p> </td>

&LT;TD width= "76%" height= "" valign= "Top" ><textarea rows= "4" name= "Information" cols= "" ></textarea ></td>

</tr>

<tr>

&LT;TD width= "100%" height= "colspan=" 2 ">

<p align= "center" ><input type= "Submit" value= "submitted" name= "B1" ><input type= "reset" value= "Cancel" name= "B2" ></td>

</tr>

</table>

</form>

<p> </p>

</body>


(4) Insertintodb.asp program, using the Add method in the component

<HTML>

<HEAD>

<TITLE></TITLE>

</HEAD>

<BODY>

<%dim INSERT_CD

Set INSERT_CD =server.createobject ("CD.") Cd_handle ")

Insert_cd. Add Request.Form ("ID"), Request.Form ("Name"), Request.Form ("Author"), Request.Form ("Price"), Request.Form (" Information ")

%>

<p> you have successfully added a bibliography </p>

<form name= "A" method= "POST" action= "listcdinformation.asp" >

<input type= "SUBMIT" value= "return" name= "B1" >

</form>

</body>


6, summary

Above I briefly introduced the definition of components and the use of VB to write ASP components in the method, in fact, the example above is just a very superficial example, I believe that the program written by you will be better than mine, I think if there is a chance, I will also thank how to use VC + + and Java to write COM components, Because I think, if you do not master COM components, you will not master the essence of ASP.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.