How do you instantiate multiple sql connections to the same database via threading without getting an error?
I'm running a program where I call a function which creates a database connection and then runs a stored procedure. I call this function four times from four separate threads. I get an error (Login failed for user 'sa'. The user is not associated with a trusted SQL Server connection. Exception Source: .Net SqlClient Data Provider) when I use multiple threads, but if I have the threads run one after another I have no problem.
Below is my code.
Dim comExecuteInsert As New SqlCommand
Dim comm As New SqlConnection
If (Not comm Is Nothing) Then
comm = Nothing
End If
comm = NewConnection(Conversion.Ser开发者_Go百科verBox.SelectedText, "TESTAAA")
Try
comm.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
comExecuteInsert.Connection = comm
comExecuteInsert.CommandType = CommandType.StoredProcedure
comExecuteInsert.CommandText = strProcedureName
comExecuteInsert.CommandTimeout = 260000
comExecuteInsert.Parameters.Add("@tableName", SqlDbType.VarChar, 100).Value = strTableName
comExecuteInsert.Parameters.Add("@filename", SqlDbType.VarChar, 500).Value = strFileName
comExecuteInsert.ExecuteScalar()
comExecuteInsert.Parameters.Clear()
comm.Close()
What version -- and more importantly, what edition -- of SQL Server are you using? Also, which version and edition of Windows are you running the SQL server on?
Some editions of SQL Server limit the number of active connections you can use at the same time, and some editions of Microsoft Windows limit the number of inbound connections that can be made at the same time.
I'm not exactly sure what the limits are. I'm sure the numbers are documented somewhere on Microsoft.com, though.
If you are using Windows 2008 Server and SQL Server 2008 Enterprise, you should be able to get thousands of simultaneous connections (you might have to worry about transactions and performance, though).
On the other hand, if you are using SQL Server Developer Edition on Windows XP Home Edition, you might not be able to have more than 5 connections at once. If ANYTHING ELSE is connecting to either Windows or SQL, that might count against your limit too - is someone using Remote Console to look at that computer? Do you have the drive mapped to your computer?
精彩评论