Questions tagged [sqlconnection]

Represents an open connection to a SQL Server database.

SqlConnection is a class in .Net's System.Data.SqlClient library that represents a connection between the local machine and a SQL Server database.

The connection can be configured using its ConnectionString property, and must be opened before use (and closed or disposed after use). ADO.NET also implements pooling for the internal connection used by SqlConnection, so calling Open is relatively cheap.

ADO.NET does not provide thread safety for SqlConnection, nor any of the objects that use it (such as SqlCommand or SqlDataReader) - these objects should be used on the same thread as the SqlConnection, and by only one thread at once.

For more information on SqlConnection, see its MSDN page: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx

1086 questions
32
votes
6 answers

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "..."; connection.Open(); …
abatishchev
  • 98,240
  • 88
  • 296
  • 433
26
votes
5 answers

Best practice for reusing SqlConnection

I've come from Java experience and am trying to start with C#. I've read SqlConnection SqlCommand SqlDataReader IDisposable and I can understand that the best practice to connecting to a DB is wrapping SqlConnection, SqlCommand and SqlDataReader in…
Hikari
  • 3,797
  • 12
  • 47
  • 77
23
votes
4 answers

C# DbConnection cast to SqlConnection

I found this piece of code in one application Database database = DatabaseFactory.CreateDatabase("connection string"); DbConnection connection = database.CreateConnection(); connection.Open(); SqlConnection sqlConnection =…
Darqer
  • 2,847
  • 9
  • 45
  • 65
23
votes
6 answers

C# connect to database and list the databases

Possible Duplicate: SQL Server query to find all current database names I am trying to figure out how to list the databases after connecting to the servers without specifying a database first. sqlConnection1 = new SqlConnection("Server=" +…
user1158745
  • 2,402
  • 9
  • 41
  • 60
23
votes
3 answers

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

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…
smdrager
  • 7,327
  • 6
  • 39
  • 49
21
votes
2 answers

Does DataAdapter.Fill() close its connection when an Exception is thrown?

I am using ADO.NET (.NET 1.1) in a legacy app. I know that DataAdapter.Fill() opens and closes connections if the connection hasn't been opened manually before it's given to the DataAdapter. My question: Does it also close the connection if the…
motto
  • 1,305
  • 3
  • 16
  • 30
20
votes
2 answers

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

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); …
Curtis
  • 101,612
  • 66
  • 270
  • 352
20
votes
2 answers

Why do we need to set Min pool size in ConnectionString

For SQL connection pool, why do we need to set up a min pool size? As connections will be saved in the connection pool and reused, why do we need to keep live connections specified by the min pool size? Thanks.
Helic
  • 907
  • 1
  • 10
  • 25
19
votes
3 answers

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

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…
EZ.
  • 223
  • 1
  • 5
  • 10
18
votes
8 answers

Why call SqlClient.SqlDataReader Close() method anyway?

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…
Julius A
  • 38,062
  • 26
  • 74
  • 96
18
votes
9 answers

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

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…
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
17
votes
3 answers

C# SQLConnection pooling

Can anyone brief me how to do Connection Pooling in ADO.Net, I do need to connect to 3 separate databases. 2 of them are in same server and the other in a separate one. Better with code snipts..
Buddhi Dananjaya
  • 643
  • 2
  • 12
  • 32
17
votes
4 answers

"Login failed for user" C# with SQLConnection

I've been trying to connect to my database (which is on the same computer as my code) through my C# code. The problem is I keep getting the "Login failed for user " "" error... I admit that my knowledge of connecting to databases is minimal and I've…
Lourens
  • 191
  • 1
  • 1
  • 5
16
votes
6 answers

.net SqlConnection not being closed even when within a using { }

Please help! Background info I have a WPF application which accesses a SQL Server 2005 database. The database is running locally on the machine the application is running on. Everywhere I use the Linq DataContext I use a using { } statement, and…
Ash
  • 5,057
  • 7
  • 35
  • 49
16
votes
2 answers

Connection.open for hangs indefinitely, no exception is thrown

When I try to do the following code, the program hangs indefinitely. I don't know why and there seems to be other unanswered topics on the matter. Although, if the IP\website cannot be reached, then it works as intended. private void DoStuff() …
1
2
3
72 73