4

im working on a login server in c# with mysql and i've a question: I've seen on the internet that the correct way to use MySqlConnection and doing queries is using the "using" statment like this:

using (SqlConnection con = new SqlConnection(CNN_STRING))
{
    using (SqlCommand cmd = new SqlCommand("COMMAND", con))
    {
        //do something
    }
}

Well, if i use this the connection will be closed and opened every time, right? The problem is that opening the connection takes about 200ms, will it take this time every time that i try to do a query?

4
  • 1
    You normally just open it once, then do all your queries. No need to open it multiple times. Commented Oct 28, 2015 at 22:06
  • 3
    "using" is the correct way to handle objects that implement IDisposable. Commented Oct 28, 2015 at 22:09
  • See Connection Pooling in MySql Commented Oct 28, 2015 at 22:11
  • 2
    @Bxx No - connections are pooled in .NET, so creating one isn't an expensive process. Keeping a connection open for long periods can cause issues, though. Commented Oct 28, 2015 at 22:15

3 Answers 3

4

Well, if i use this the connection will be closed and opened every time, right?

It will be closed automatically. You still have to open it before you use it.

The problem is that opening the connection takes about 200ms, will it take this time every time that i try to do a query?

You'll have to try it and measure it to know for sure, but connections are pooled in .NET, so creating one isn't an expensive process. Whether opening one will take the same amount of time each time cannot be determined without trying it, since it depends on the quality of your network.

Try it the recommended way, and if it becomes a measurable problem for your app overall then look for other solutions.

Do NOT go to the other end of the spectrum where you keep a connection open for the life of your app. If you do, then you constantly have to check to make sure the connection is still open (since it can be closed by other means) and can cause other issues.

Sign up to request clarification or add additional context in comments.

2 Comments

upvote. Last paragraph - ditto. I'm fed up with our code - all the Open and Closeings, especially when using using.
not to mention... orphaned connections.
0

Nope! Just make sure the logic you're using is within that using block. It will be connected to and disposed of correctly.

1 Comment

this is not an answer also using takes care of auto-disposing
0

This should open your connection and dispose it properly at the end of the using block.

using (SqlConnection con = new SqlConnection(CNN_STRING))
{
    SqlCommand cmd = new SqlCommand("COMMAND, con);
    //do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.