sqlconnection

Will SqlConnection get disposed by GC?

依然范特西╮ 提交于 2019-11-27 14:50:27
问题 Disclaimer: I know IDisposable should be implemented when dealing with unmanaged resources. The rest of the code should be deterministic and do using (...) { } (equivalent of try {} finally { Dispose(); } ) to guarantee a cleanup as soon as possible. Also, the GC will not call Dispose(), so the recommended pattern is to override the Finalize() method (in C# using the destructor syntax) which then calls Dispose() . The GC will usually call Finalize() (unless GC.SuppressFinalize() has been

Why call SqlClient.SqlDataReader Close() method anyway?

大兔子大兔子 提交于 2019-11-27 14:38:23
问题 Is the SqlClient.SqlDataReader a .NET managed object or not? Why do we have to call the Close() method explicitly close an open connection? Shouldn't running out of scope for such an object automatically close this? Shouldn't garbage collector clean it up anyway? Please help me understand what is the best practise here. I have seen a related question here and it further illustrates the issue I have with a web application. The issue is that we were running out of connections. The detailed

What is the proper way to ensure a SQL connection is closed when an exception is thrown?

岁酱吖の 提交于 2019-11-27 14:38:13
问题 I use a pattern that looks something like this often. I'm wondering if this is alright or if there is a best practice that I am not applying here. Specifically I'm wondering; in the case that an exception is thrown is the code that I have in the finally block enough to ensure that the connection is closed appropriately? public class SomeDataClass : IDisposable { private SqlConnection _conn; //constructors and methods private DoSomethingWithTheSqlConnection() { //some code excluded for brevity

Is it best to pass an open SqlConnection as a parameter, or call a new one in each method?

非 Y 不嫁゛ 提交于 2019-11-27 13:39:10
If methods/functions I'm going to call involve the need of an open SqlConnection, I will open this up in the method which is calling the function. For example: protected static void btnSubmit(){ conn.Open(); myMethod(someParam, conn); conn.Close(); } protected static void myMethod(object someParam, SqlConnection conn){ //Some SQL commands etc here.. } I do this so that I: Only ever open and close 1 SqlConnection per process However, would it be better to structure my code like so: protected static void btnSubmit(){ myMethod(someParam); } protected static void myMethod(object someParam){

When should I open and close a connection to SQL Server

本秂侑毒 提交于 2019-11-27 09:41:38
问题 I have a simple static class with a few methods in it. Each of those methods open a SqlConnection, query the database and close the connection. This way, I am sure that I always close the connection to the database, but on the other hand, I don't like to always open and close connection. Below is an example of what my methods look like. public static void AddSomething(string something) { using (SqlConnection connection = new SqlConnection("...")) { connection.Open(); // ... connection.Close()

Is it better to execute many sql commands with one connection, or reconnect every time?

*爱你&永不变心* 提交于 2019-11-27 07:12:22
Here's my test code, which seems to suggest that it's better to connect multiple times instead of connecting just once. Am I doing something wrong? int numIts = 100; Stopwatch sw = new Stopwatch(); sw.Start(); using (SqlConnection connection = new SqlConnection(connectionParameters)) { connection.Open(); for(int i = 0; i < numIts; i++) { SqlCommand command = new SqlCommand(sqlCommandName, connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue(par1Name, par1Val); command.Parameters.AddWithValue(par2Name, par2Val); using(SqlDataReader reader = command

How to get last executed SQL query by SqlConnection?

99封情书 提交于 2019-11-27 06:23:27
问题 Actually, my scenario is bit different than mentioned here. I have asked other question. But as I am not getting solution there, I decided to change the approach. I have a SqlConnection object accessible to my code. All other ADO.NET objects like SqlCommand , SqlParameter etc are not accessible to me. These other objects are consumed by Dapper Extensions ORM. My application executes SQL queries using SqlConnection object and Dapper Extensions method. SQL query is auto generated by Dapper

Sanitize table/column name in Dynamic SQL in .NET? (Prevent SQL injection attacks)

萝らか妹 提交于 2019-11-27 05:26:00
I am generating some Dynamic SQL and would like to ensure that my code is safe from SQL injection . For sake of argument here is a minimal example of how it is generated: var sql = string.Format("INSERT INTO {0} ({1}) VALUES (@value)", tableName, columnName); In the above, tableName , columnName , and whatever is bound to @value come from an untrusted source. Since placeholders are being used @value is safe from SQL injection attacks, and can be ignored. (The command is executed via SqlCommand.) However, tableName and columnName cannot be bound as placeholders and are therefor vulnerable to

How to use ADO.net Entity Framework with an existing SqlConnection?

十年热恋 提交于 2019-11-27 05:25:45
问题 I have an existing asp.net website that uses an SqlConnection. I have added the ADO.net Entity Framework. I have successfully connected to the database and created the .edmx file. I am able to connect through the Entity Framework with the connectionstring that is automatically generated. I want to use the existing SqlConnection object that I use throughout the site for the Entity Framework connection. I do not want to have to use a second database connection for the one page that is going to

Why doesn't Dapper dot net open and close the connection itself?

谁说我不能喝 提交于 2019-11-27 04:47:13
Dapper implicitly expects a connection to be open when it uses it. Why doesn't it open and close it itself? Wouldn't this simply connection management? I ask because a co-worker and I have been going back and forth on the nature of what goes on behind the scenes with connection pooling, and if there is any benefit to keeping a connection open amongst multiple commands, or to open and close it for each command. Dapper now (and for quite some time) deals with this internally. It just works™ Original (outdated) answer: You aren't wrong. The reason I hadn't noticed this inconvenience is that for