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
208
votes
3 answers

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

What does it mean for an SqlConnection to be "enlisted" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted…
Triynko
  • 18,766
  • 21
  • 107
  • 173
150
votes
8 answers

in a "using" block is a SqlConnection closed on return or exception?

First question: Say I have using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string storedProc = "GetData"; SqlCommand command = new SqlCommand(storedProc, connection); command.CommandType…
Marcus
  • 5,407
  • 3
  • 31
  • 54
134
votes
6 answers

"open/close" SqlConnection or keep open?

I have my business-logic implemented in simple static classes with static methods. Each of these methods opens/closes SQL connection when called: public static void DoSomething() { using (SqlConnection connection = new SqlConnection("...")) …
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
127
votes
9 answers

Check if SQL Connection is Open or Closed

How do you check if it is open or closed I was using if (SQLOperator.SQLCONNECTION.State.Equals("Open")) however, even the State is 'Open' it fails on this check.
user222427
124
votes
8 answers

Do I have to Close() a SQLConnection before it gets disposed?

Per my other question here about Disposable objects, should we call Close() before the end of a using block? using (SqlConnection connection = new SqlConnection()) using (SqlCommand command = new SqlCommand()) { command.CommandText = "INSERT…
John B
  • 20,062
  • 35
  • 120
  • 170
98
votes
6 answers

What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET?
Dhanapal
  • 14,239
  • 35
  • 115
  • 142
94
votes
10 answers

Changing SqlConnection timeout

I am trying to override the default SqlConnection timeout of 15 seconds and am getting an error saying that the property or indexer cannot be assigned because it is read only. Is there a way around this? using (SqlConnection connection = new…
Haymak3r
  • 1,321
  • 1
  • 11
  • 17
82
votes
10 answers

SQL Server returns error "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'." in Windows application

An application that has been working without problem (and has not had any active development done on it in about 6 months or so) recently began failing to connect to database. Operations admins cant say what might have changed that would cause the…
CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
62
votes
9 answers

How to run multiple SQL commands in a single SQL connection?

I am creating a project in which I need to run 2-3 SQL commands in a single SQL connection. Here is the code I have written: SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\project.mdf;Integrated…
user1831272
  • 683
  • 2
  • 7
  • 6
61
votes
4 answers

Does SqlCommand.Dispose close the connection?

Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. …
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
48
votes
5 answers

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

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…
user420667
  • 6,552
  • 15
  • 51
  • 83
44
votes
4 answers

How to use the ConfigurationManager.AppSettings

I've never used the "appSettings" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the "ConnectionStrings" SqlConnection con = new SqlConnection(); con.ConnectionString =…
jorame
  • 2,147
  • 12
  • 41
  • 58
40
votes
6 answers

When should I open and close a connection to SQL Server

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…
Martin
  • 39,309
  • 62
  • 192
  • 278
37
votes
3 answers

Should I be using SqlDataReader inside a "using" statement?

Which of the following two examples are correct? (Or which one is better and should I use) In the MSDN I found this: private static void ReadOrderData(string connectionString) { string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;" …
Keoki
  • 535
  • 2
  • 6
  • 11
35
votes
2 answers

When does "SqlConnection does not support parallel transactions" happen?

I have a ton of rather working code that's been here for months and today I saw the following exception logged: System.InvalidOperationException SqlConnection does not support parallel transactions. at…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
1
2 3
72 73