Use COM components to add, delete, and modify web data

Source: Internet
Author: User

1. test environment: VB6.0 + SQL2000 + asp3.0 + WINXP + iis5.1
2. Create three stored procedures:
A. Add a user
Create proc add_user
@ Username varchar (20 ),
@ Password varchar (20)
As
Insert into userinfo (username, password) values (@ username, @ password)
Go
B. modify a user
Create proc modify_user
@ Username varchar (20 ),
@ Password varchar (20 ),
@ ID int
As
Update user_info set username = # username, password = # password where id = # ID
Go

C. delete a user
Create proc del_user
@ ID varchar (20)
As
Delete from userinfo where id = # ID
Go
Note: replace # @
3. create COM components
Open VB6.0, create an active DLL project, name the project Fuser, and name the class userinfo.
Write the following code:
Public Function adduser (byval username as string, byval password as string) as Boolean
Dim conn as new ADODB. Connection 'creates An ADO connection object. Here, a new entry is equivalent to creating an instance.
Conn. Open "provider = sqloledb; user id = sa; Password = 123; database = jsjdjks; server = 127.0.0.1" 'it's easy to connect to SQL data
Library, connected by SA and password

Set cmd = new ADODB. Command 'creates the command object. Because I use the stored procedure, this is required.
Cmd. activeconnection = conn' connection parameter value
Cmd. commandtype = adw.storedproc 'indicates that the stored procedure type is executed.
Cmd. commandtext = "add_user" 'name of the stored procedure called

Cmd. Parameters. append cmd. createparameter ("username", advarchar, adparaminput, 20, username) 'external parameter-oriented Stored Procedure
Transfer numeric value
Cmd. Parameters. append cmd. createparameter ("password", advarchar, adparaminput, 20, password) 'external parameters are stored in the process
Transfer numeric value
Set rs = cmd. Execute 'execute the Stored Procedure

If Rs. EOF then ', check whether the execution is successful.
Adduser = false' execution failed
Else
Adduser = true' execution successful
End if

Rs. Close 'close the object and release the memory
Set rs = nothing 'same as above
Set cmd. activeconnection = nothing 'Close the database connection
End function' function end
Public Function modifyuser (byval username as string, byval password as string, byval ID as integer) as Boolean
Dim conn as new ADODB. Connection 'creates An ADO connection object. Here, a new entry is equivalent to creating an instance.
Conn. Open "provider = sqloledb; user id = sa; Password = 123; database = jsjdjks; server = 127.0.0.1" 'it's easy to connect to SQL data
Library, connected by SA and password

Set cmd = new ADODB. Command 'creates the command object. Because I use the stored procedure, this is required.
Cmd. activeconnection = conn' connection parameter value
Cmd. commandtype = adw.storedproc 'indicates that the stored procedure type is executed.
Cmd. commandtext = "modify_user" 'name of the stored procedure called

Cmd. Parameters. append cmd. createparameter ("username", advarchar, adparaminput, 20, username) 'external parameter-oriented Stored Procedure
Transfer numeric value
Cmd. Parameters. append cmd. createparameter ("password", advarchar, adparaminput, 20, password) 'external parameters are stored in the process
Transfer numeric value
Cmd. Parameters. append cmd. createparameter ("ID", advarchar, adparaminput, 20, ID)
Set rs = cmd. Execute 'execute the Stored Procedure

If Rs. EOF then ', check whether the execution is successful.
Modifyuser = false' execution failed
Else
Modifyuser = true' execution successful
End if

Rs. Close 'close the object and release the memory
Set rs = nothing 'same as above
Set cmd. activeconnection = nothing 'Close the database connection
End Function
Public Function deluser (byval ID as integer) as Boolean
Dim conn as new ADODB. Connection 'creates An ADO connection object. Here, a new entry is equivalent to creating an instance.
Conn. Open "provider = sqloledb; user id = sa; Password = 123; database = jsjdjks; server = 127.0.0.1" 'it's easy to connect to SQL data
Library, connected by SA and password

Set cmd = new ADODB. Command 'creates the command object. Because I use the stored procedure, this is required.
Cmd. activeconnection = conn' connection parameter value
Cmd. commandtype = adw.storedproc 'indicates that the stored procedure type is executed.
Cmd. commandtext = "del_user" 'name of the stored procedure called

Cmd. Parameters. append cmd. createparameter ("ID", advarchar, adparaminput, 20, ID)
Set rs = cmd. Execute 'execute the Stored Procedure

If Rs. EOF then ', check whether the execution is successful.
Deluser = false' execution failed
Else
Deluser = true' execution successful
End if

Rs. Close 'close the object and release the memory
Set rs = nothing 'same as above
Set cmd. activeconnection = nothing 'Close the database connection
End Function
4. Call the COM Component

After fuser. dll is generated, register it in the run and enter the command regsvr32 D:/fuser. dll.
Call the following method to add data:
<%
Username = request ("username ")
Password = request ("password ")
Dim OBJ
Set OBJ = server. Createobject ("fuser. userinfo ")
If objjadduser (username, password) then
Response. Write ("added successfully! ")
End if
%>
Modify the data and call it like this:
<%
Username = request ("username ")
Password = request ("password ")
Id = request. querystring ("ID ")
Dim OBJ
Set OBJ = server. Createobject ("fuser. userinfo ")
If objj. modifyuser (username, password, ID) then
Response. Write ("modification successful! ")
End if
%>
To delete data, call:
<%
Id = request. querystring ("ID ")
Dim objj
Set objj = server. Createobject ("fuser. userinfo ")
If objj. deluser (ID) then
Response. Write ("deleted successfully! ")
End if
%>

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.