Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen connection while another connection is open
    primarykey
    data
    text
    <p>i need to call a function that do some query while another connection is opened and its doing a transaction.</p> <p>Ok i get this is weird, here some code:</p> <p>Main part:</p> <pre><code>Using connection As New SqlConnection(connectionString) connection.Open() Dim command As SqlCommand = connection.CreateCommand() Dim transaction As SqlTransaction transaction = connection.BeginTransaction("myTransaction") command.Connection = connection command.Transaction = transaction command.CommandText = sSQL Try command.ExecuteNonQuery() Dim functionResult As String = myFunction(param1, param2) If functionResult &lt;&gt; "" Then 'error! i need to rollback the first query done here! transaction.Rollback() else transaction.Commit() End If Catch ex As Exception transaction.Rollback() End Try End If End Using </code></pre> <p><em>myFunction</em> do lot of stuff, and a lot of querys. Every query needs to reopen connection (without transaction this time) but everytime i try to execute the first query inside my function i got timeout error from database (after 30 seconds).</p> <p>I know i can do this work "copy-pasting" all the myFunction code inside that already opened connection and using the already opened connection, but i use that function more than once and i don't want to mess up my code.</p> <p>How can i solve this?</p> <p>edit for more information: that was an already reduced version of the code i'm using, but here a reduced version on what "myFunction" do:</p> <pre><code>Dim connectionString As String = "my connection string" Dim queryString As String = "SELECT id FROM foo WHERE param1 = @myValue" Dim ds As DataSet = New DataSet() Try Using connection As New SqlConnection(connectionString) Dim command As New SqlCommand(queryString, connection) connection.Open() command.CommandText = queryString command.Parameters.Add("@myValue", SqlDbType.Int).Value = 10 Dim adapter As New SqlDataAdapter() adapter.SelectCommand = command adapter.Fill(ds, "randomName") If ds.Tables("randomName").Rows.Count &lt; 0 Then 'error! connection.Close() Return "error" End If End Using Catch ex As Exception Return "Database error - " &amp; ex.Message End Try </code></pre> <p>The code execution (even in debug) freeze on the <code>adapter.Fill(ds, "randomName")</code> command for 30 seconds, after that i get a timout error</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload