sql - SqlConnection.Open() Establishing a connection EVERY query? -


In my Winfix app that uses a remote database, I have the function below (I also have two other functions Those who work equally: For a scalar query that returns zero to zero failure, for other updates and insertions, which is wrong on failure.)

Currently, all Data manipulation is pumped through these three functions.

It works fine, but please advise overall that if I launch my app better than setting a connection, then the app is killed as it is closed? Or at any other time? (Again, this is a windows app, so it has the ability to sit with a stability, while a user takes a long lunch break.)

So far, I do not see any bad effects As everything seems to be "in the blink of an eye" ... but is I slowing down the data or are there any other potential dangers, such as memory leaks? Please make a notification that any connection is closed even if the function is terminated.

Open () da = New SQL Data Adapter (Query, Conn.) Con Close () da Catch as Fill (DT) exchange debug. Print (query) MSBBX ("unable to perform recertive data" and VBCRFLF & Amp; vbCrLf and previous message, MsgBoxStyle.Critical, "Unable to retrieve data.") Try end.Display () Con.Close () Return dt There are some exceptions in the end Function

but in the best practices, Net is actually the highest Demands for creating a new connection object for questions. Actually.

In addition to the benefits made by connection pooling (discussed in the comments above for your question), it becomes easy to scale your app to multiple threads using a new connection object. Imagine writing an app that used to try to rely on a global connection object, and then suddenly want a separate thread to work on a long-running job in the background, it only shows that It has been blocked, or it is blocking other normal access, worse than for the database, imagine trying to do this for a web app, and do it wrong, such as Your entire application is shared for the domain (all users on the site)

So it's something that corrects your existing code.
However, there are two serious problems with the current method.

The first is that the author does not have to understand that when to open and when should I close one association. This is complicated using the method, because 1 is not a good reason to see a call when using this method. Open () , . Close () , or Dispute () anywhere in that method When not using the method, the connection must always be closed as a end block: and the easiest way to do this is to use < / Code> block

The second SQL injection is written in a written form, there is no way to include the parameter data in the query. It only allows a complete SQL command string. This practically forces you to write which will be very weak for SQL injection attacks. If you do not already know what attacks of SQL injection are, stop what you are doing and spend some time in goggling that phrase.

I suggest an alternative method to solve these problems: Datamate as new datatyla () results in the form of new SqlConnection as encoded (as stringed as SqlParameter, ParamArray parameter (as in the form of Byval query); The new SqlCommand (Query, Con), as is the new SqlDataAdpapter (CMD) parameter, IsNot as nothing else if / CMD. Try Parameters.AddRange (parameter) da. Fill (Results) Exception As Debug Hold. Better to allow a high-level method to present the print (query) to the user 'better' just log it in here and Reithro may have so much presentation-level throwing and end use 'Guarantees Connection is not open

1 See the first paragraph of the "Comments" section.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -