Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, I am using .NET Remoting for database connection.

 

I have three components:

 

1. A class library 'RemoteComponent.dll':

 

Imports System

Imports System.Data.SqlClient

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

 

Public Class RemoteClass

Inherits MarshalByRefObject

 

Public Function ConnectionString() As String

Return "user id=sa;password=;Database=Northwind;Server=localhost;Connect Timeout=30"

End Function

 

Public Overloads Function RunSQL( _

ByVal cSQL As String, _

ByVal cTableName As String) As DataSet

 

Dim oCmd As SqlCommand = New SqlCommand

Dim oCnn As SqlConnection = Nothing

Dim oAdp As SqlDataAdapter = New SqlDataAdapter

Dim oDs As DataSet = New DataSet

 

Try

oCnn = New SqlConnection(ConnectionString)

 

With oCmd

.Connection = oCnn

.CommandText = cSQL

.CommandType = CommandType.Text

End With

 

With oAdp

.SelectCommand = oCmd

.Fill(oDs, cTableName)

End With

 

If Not oCnn Is Nothing And oCnn.State <> ConnectionState.Closed Then

oCnn.Close()

End If

 

Return oDs

 

Catch ex As Exception

Throw New System.Exception(ex.Message)

End Try

End Function

End Class

 

2. A console application 'Listener.exe':

 

Imports System

imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

Imports System.Runtime.Remoting.Channels.Http

 

Public Class Listener

Public Shared Function Main(ByVal args() As String) As Integer

Dim channel As TcpChannel = New TcpChannel(8080)

ChannelServices.RegisterChannel(channel)

 

RemotingConfiguration.RegisterWellKnownServiceType( _

GetType(RemoteComponent.RemoteClass), _

"RemoteComponent.RemoteClass", _

WellKnownObjectMode.SingleCall)

 

System.Console.WriteLine("Press the enter key to exit...")

System.Console.ReadLine()

Return 0

End Function

End Class

 

3. A windows application client.exe:

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim chan As TcpChannel = New TcpChannel

ChannelServices.RegisterChannel(chan)

 

Dim oRemoteClass As RemoteComponent.RemoteClass = CType(Activator.GetObject( _

GetType(RemoteComponent.RemoteClass), _

"tcp://omega:8080/RemoteComponent.RemoteClass"), RemoteComponent.RemoteClass)

 

If oRemoteClass Is Nothing Then

System.Console.WriteLine("Error: unable to locate server")

Else

Dim cSQL As String

Dim dsData As DataSet

 

cSQL = " select *"

cSQL = cSQL & " from customers"

 

dsData = oRemoteClass.RunSQL(cSQL, "Customers")

 

With Me.ComboBox

.ValueMember = dsData.Tables("Customers").Columns("CustomerId").ColumnName

.DisplayMember = dsData.Tables("Customers").Columns("CompanyName").ColumnName

.DataSource = dsData.Tables("Customers").DefaultView

End With

 

End If

ChannelServices.UnregisterChannel(chan)

End Sub

 

I execute two or more instance of my client. In the database I see only one session (commant sp_who in SQL Server) for the NorthWind database.

 

I need one session for each instance of my client application!!!

 

Thanks for your help!!!

Posted

you are only seeing a single instance because you are using a single call and the call is happening in a single thread.

it opens . . . it closes. only one session at a time is being created.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I have a doubt

 

Ok, you are right, but I have a doubt:

 

If I don't use .NET Remoting and build the instance of RemoteClass in the following form:

 

Dim oRemoteClass As New RemoteComponent.RemoteClass

 

and If I execute two or more instances of my client application I see one session for each instance of my client application in the database.

 

The question is: Is there any way to obtain this behavior with .NET Remoting???

 

 

you are only seeing a single instance because you are using a single call and the call is happening in a single thread.

it opens . . . it closes. only one session at a time is being created.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...