PDA

View Full Version : Writting Own Component!


charon
08-16-2002, 12:54 PM
Hi, I don't know whether this forum allow to ask question about Component Object or not. Anyway I just try it.

As we all know that, COM is to facilitate code reuse, and methods for extending applications. Now, intead of writting long VB script into an asp page, a lot of web programmer already adopt COM technology for writting COM object and call it via ASP. This is not only for code reuse but fastert processing.

I'm a beginner in writting component, have alreday gone through some of the tutorial and sample about writting component. Now, intend to write a very simple Database Connection Component by using Visual Basic, but don't know how to start.

Normally, I will create an include file for database connection, as listed below:

<!-- #Include file="ADOVBS.INC" -->
<% Response.Buffer = true %>
<%
Session.Timeout=720
' Create and establish data connection
Set objDC = Server.CreateObject("ADODB.Connection")
objDC.ConnectionTimeout = 15
objDC.CommandTimeout = 30


'Use this line to use Access
objDC.Open "DBQ=" & Server.MapPath("/intra/data/launchregister.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;uid=;pwd=1234"

' Create recordset and retrieve values using the open connection
Set objRS = Server.CreateObject("ADODB.Recordset")

with objRS
.ActiveConnection = objDC
.CursorLocation = adUseClient
.CursorType=adOpenDynamic
.LockType=adLockOptimistic
end with

%>

So now, intead of creating include file, I want to create a simple connection component. Please help!

Thanks so much...

kara
08-16-2002, 02:27 PM
hi Charon , ill try to explain writing and using a simple com object briefly

Just open a new vb project has a name CHARON , add a new class , name it to CONNECTOR .

From references add , Microsoft Active Data Objects Library 2.6 or 2.7 whatever you installed

Now start to write your very own com object ;)

------------------------- cut here ------------------------

Option Explicit

Public Conn As New ADODB.Connection
Public Command As New ADODB.Command

' Connect to DB
Public Sub ConnectDB(SqlServer As String, Catalog As String, LoginUser As String, LoginPass As String)
Conn.ConnectionString = "Provider=SQLOLEDB;Data Source=" & SqlServer & "; Initial Catalog=" & Catalog & ";User Id=" & LoginUser & ";Password=" & LoginPass & ";"
Conn.ConnectionTimeout = 600
Conn.Open
Command.ActiveConnection = Conn
End Sub

' Disconnect DB
Public Sub DisconnectDB()
Conn.Close
Set Conn = Nothing
End Sub
'
Private Sub Class_Terminate()
If Not Conn.State = adStateClosed Then
Conn.Close
Set Conn = Nothing
End If
End Sub

------------------------- cut here ------------------------

From properties windows change your class' Instancing propertity to MultiUse , and change your projects' threading model ( from properties ) to Apertment Model

Save your project as CHARON.vbp and save your class clsConnector.cls ( or something else :) ) , from file menu
click to "Make Charon.dll" , save your dll to public place for else in cgi-bin folder under inetpub with a name CharonConnector.dll

Register it with ( from command prompt )
regsvr32 c:\inetpub\cgi-bin\CharanConnector.dll

Open your asp file ,

<%
Dim objCharon
Set objCharon = Server.CreateObject("CHARON.Connector")

objCharon.ConnectDB ("172.12.23.23" , "TESTDB" ,"sa","pass")

SQL ="Select something else"
Set Rs = objCharan.Conn.Execute(SQL)
...
...
objCharon.DisconnectDB

Set objCharon = nothing

%>


That's all .

I wrote ConnectDB method for SQL SERVER connection , you can easly change it for access , oracle etc...

:thumbsup:

charon
08-17-2002, 06:58 AM
thanks kara for the reply....
As I mentioned before, I have gone through some of the tutorial and samples about writting component. I feel so confuse, some are given a very simple example, some very complicated whereby it mentioned about the interface of the COM object. It is not as easy/simple as some others sample which is very straight forward as yours. I said they are quite straight forward is because the way that the component is coded is same as we write function/procedure in our asp page. Just for example:
A function might be as simple as below:

fucntion Calculate ( a )
a = a + 1
end fucntion


and the above code can be cut & paste into VB and save it as dll file as a component object. I did study "Beginning of COM Object" from wrox, it sound very difficult as it got mentioned about the interface and the sample code is not as easy as yours.

Can you explain to me???

Thanks!