Duplicate Connections

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the SQLConnection object to establish a connection a SQL Server
2000 PC running on my local PC. Whenever I open the connection I get two
loggins on the SQL Server. Does anyone know what is causing this to happen?
 
I just ran a test using the following code and it created a single connection
and only a single SPID as shown in SQL Server Ent Mgr's Current Activity
view. What does your code look like?

string sConnString = "Server=MyServer;Database=Northwind;Integrated
Security=True";
string sSql = "SELECT CategoryID, CategoryName FROM Categories ORDER BY
CategoryName";
using (SqlConnection cn = new SqlConnection(sConnString))
using (SqlCommand cmd = new SqlCommand(sSql, cn))
{
cn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
while (rdr.Read())
{
// do something in here
}
}



// John Papa
// http://codebetter.com/blogs/john.papa
 
Here's the basic idea of my code block. Each time I run this, I see two login
events using the SQL Profiler. If I use OLEDB, I only get one. The second
login event is fired when I run the ExecuteReader command. The funning thing
is that even though it creates a second connection to the database, the
actual query runs on the original login connection.

Dim mySql As SqlConnection = New
SqlConnection("SERVER=.;DATABASE=Northwind;USER
ID=sqluser;PASSWORD=password;TRUSTED_CONNECTION=false")
Dim myCmd As SqlCommand = New SqlCommand("spGetAllEmployees", mySql)
myCmd.CommandType = CommandType.StoredProcedure
Dim myDR As SqlDataReader
Try
mySql.Open()
myDR = myCmd.ExecuteReader()
While myDR.Read()
'Do something
End While
Catch ex As Exception

End Try
 
Its due to connection pooling. Ignore it.

- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/



DavidM said:
Here's the basic idea of my code block. Each time I run this, I see two login
events using the SQL Profiler. If I use OLEDB, I only get one. The second
login event is fired when I run the ExecuteReader command. The funning thing
is that even though it creates a second connection to the database, the
actual query runs on the original login connection.

Dim mySql As SqlConnection = New
SqlConnection("SERVER=.;DATABASE=Northwind;USER
ID=sqluser;PASSWORD=password;TRUSTED_CONNECTION=false")
Dim myCmd As SqlCommand = New SqlCommand("spGetAllEmployees", mySql)
myCmd.CommandType = CommandType.StoredProcedure
Dim myDR As SqlDataReader
Try
mySql.Open()
myDR = myCmd.ExecuteReader()
While myDR.Read()
'Do something
End While
Catch ex As Exception

End Try
happen?
 
Back
Top