logo
down
shadow

Best practice for reusing SqlConnection


Best practice for reusing SqlConnection

By : user2949180
Date : November 16 2020, 06:23 AM
Hope this helps Creating a new instance of the class SqlConnection does not create a new network connection to SQL Server, but leases an existing connection (or creates a new one). .NET handles the physical connection pooling for you.
When you have finished with your connection (through which you can send multiple queries) just Close() or Dispose() (or use a using{} block preferably).
code :


Share : facebook icon twitter icon
Reusing SqlConnection within a DAL method

Reusing SqlConnection within a DAL method


By : preeti
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The real problem is not the connection, but the transactions. When a logical operation involves multiple DAL physical operations, usually they have to be part of a transcation. Unless corectness is optional... If a transaction spans multiple connections then it has to be elevated to a distributed transaction, with disastrous perofrmance results. So when designing a DAL, always thrive to affinitize a transaction to a connection. This ripples through the DAL API design, as usually the result is that the conneciton and transaction objects have to be explictly handed out to the DAL methods, either as individual parameters or as a 'context' object that aggregates them.
Proper way of reusing SqlConnection for page

Proper way of reusing SqlConnection for page


By : dianitica
Date : March 29 2020, 07:55 AM
this one helps. Sql Connections are pooled so if you are concerned with performance; chances are that both methods will use just one connection and therefore, the penalty for using the second approach is negligible.
On the other hand, the second approach shows better encapsulation and separation of concerns. I would go with the second option.
SqlConnection in C# - Safe programming practice

SqlConnection in C# - Safe programming practice


By : Mishra Airy
Date : March 29 2020, 07:55 AM
this one helps.
Is this just because they were being brief with the code, or is there a reason they're not worth handling here? are they possibly handld by the using construct in some way?
Reusing SqlConnection in multiple ninject bindings

Reusing SqlConnection in multiple ninject bindings


By : Rubels69
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Create a binding for your SqlConnection rather than instantiating one:
code :
Bind<SqlConnection>().ToConstant(new SqlConnection(ConfigurationManager.ConnectionStrings["widgetsConn"].ToString()));

Bind<IRepository<Widget>>().To<SqlRepository<Widget>>().InSingletonScope().WithConstructorArgument("connection", context => Kernel.Get<SqlConnection>());
Bind<IRepository<Credential>>().To<SqlRepository<Credential>>().InSingletonScope().WithConstructorArgument("connection", context => Kernel.Get<SqlConnection>());
Is it bad practice to wrap a new SqlConnection instance in a static method?

Is it bad practice to wrap a new SqlConnection instance in a static method?


By : aprit
Date : March 29 2020, 07:55 AM
To fix the issue you can do The method is fine but all the try/catch/finally is not needed. This is what "using" does. Let alone that you have potential null reference if the conn object is not constructed successfully.
Related Posts Related Posts :
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • Using same alias for multiple namespaces
  • how to move mails into separate folders
  • Multithreaded Uploader
  • Memory fragmentation?
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org