Best way to handle connection open and close with a For loop
NickName:Adam Ask DateTime:2017-01-24T09:41:51

Best way to handle connection open and close with a For loop

I've already checked here.

I am looping an opening a connection each time and I'm not sure if ASP.NET handles it with a performance hit or that it recognizes this code and optimizes it automatically. What I have now:

For i As Integer = 0 To 100
    cmd = New SqlCommand("UPDATE <table> where id=@id", myConnection)
    cmd.Parameters.Add(New SqlParameter("@id", i))
    Try
        myConnection.Open()
        cmd.ExecuteNonQuery()
    Catch ex As Exception
    Finally
        myConnection.Close()
    End Try
Next i

How could I alter this code so that it does not open a connection each time? Bring the closing and opening outside the For loop? Do a check on the existence of an open connection within the loop? I'd love to see the code sample for the best practice.

Copyright Notice:Content Author:「Adam」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41818661/best-way-to-handle-connection-open-and-close-with-a-for-loop

More about “Best way to handle connection open and close with a For loop” related questions

Best way to handle connection open and close with a For loop

I've already checked here. I am looping an opening a connection each time and I'm not sure if ASP.NET handles it with a performance hit or that it recognizes this code and optimizes it automatical...

Show Detail

Best way to open/close DB Connection with async/await

In tutorials I found, there is always opening and closing the connection for every request, for example : import asyncio import asyncpg async def run(): conn = await asyncpg.connect(user='use...

Show Detail

Proper way to open and close connection in loop

I have a code like below : public void Do { using (var myConnection = new SqlConnection(connectionString)) { for (int i = 0; i &lt; 4; i++) { MyPro...

Show Detail

open and close mysql connection multiple time

I want to write a function for mysql connection to use it any where. there is tow way in my mind 1)in top of function open connection and execute query and close connection. in this way each query...

Show Detail

open/close an oracle connection multiple times c#

In my business logic, I use multiple oracle query's multiple times. What is the best way to open and close the oracle connection? private void update() { OracleConnection con = new OracleConn...

Show Detail

To close or not to close an Oracle Connection?

My application have performance issues, so i started to investigate this from the root: "The connection with the database". The best practices says: "Open a connection, use it and close is as soon...

Show Detail

Best way to manage database connection for a Java servlet

What is the best way to manage a database connection in a Java servlet? Currently, I simply open a connection in the init() function, and then close it in destroy(). However, I am concerned that "

Show Detail

Start and close SQL connection - At start and in the end or every time needed?

I'm making a web application in C# with ASP.Net, and I'm using an SQL database for it. In the guides I saw (about SqlConnection and SqlCommand) they open and close the SQL connection each time they...

Show Detail

Correct way to open and close a mongodb connection python?

I have a python script that uses multiprocessing to update a mongodb database and I need some help determining when is the optimal time to close my mongodb connection. For context, this is what my ...

Show Detail

Is it inefficient to open and close a WebSocket connection multiple times?

I'm building a game that involves realtime voice chat in which I want to use WebSockets for. My understanding is that WebSockets are not scaleable meaning they would require a lot of server

Show Detail