Dealing with .NET Enterprise Services: .NET Remoting, Web Services and Service Components - Working with a database-based COM application
(Page 4 of 6 )
Let us go through the following program.
<ClassInterface(ClassInterfaceType.AutoDispatch), Transaction
(TransactionOption.Supported), ConstructionEnabled([Default]:="Data Source=.;Initial Catalog=Northwind;User
id=sa"), JustInTimeActivation()> _
Public Class DBTest
Inherits ServicedComponent
Dim m_ConnectionString As String
Protected Overrides Sub Construct(ByVal constructString As String)
' Called after constructor
m_ConnectionString = constructString
End Sub
Public ReadOnly Property ConnectionString()
Get
Return m_ConnectionString
End Get
End Property
<AutoComplete()> _
Public Sub ExecuteDML(ByVal strSQL As String)
Dim cn As New SqlConnection(m_ConnectionString)
cn.Open()
Dim cmd As New SqlCommand(strSQL, cn)
cmd.ExecuteNonQuery()
cmd.Dispose()
cn.Close()
End Sub
End Class
You can provide a connection string within your COM+ application (which can be modified later, even at run time, using a Component Services snap-in). And to retrieve the same connection string you use the following code fragment:
Protected Overrides Sub Construct(ByVal constructString As
String)
' Called after constructor
m_ConnectionString = constructString
End Sub
And you can expose the same connection string using the following code:
Public ReadOnly Property ConnectionString()
Get
Return m_ConnectionString
End Get
End Property
The “ExecuteDML” method is the most important method of the above COM+ application. It has an attribute, “<AutoComplete>”, which says that the transaction has to commit after successful execution (or roll back if any error arises).
The client program to call the above database-based COM+ would be as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim obj As New COMPlusEx2Srv.DBTest
MessageBox.Show(obj.ConnectionString)
obj.ExecuteDML(Me.txtQuery.Text)
MessageBox.Show("Updated succesfully")
End Sub
The above code just creates an object of the COM+ application and accesses its properties (“ConnectionString”) and methods (“ExecuteDML”).
Next: Working with Web Services >>
More .NET Articles
More By Jagadish Chaterjee