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
15
votes
2 answers

Session-Per-Request with SqlConnection / System.Transactions

I've just started using Dapper for a project, having mostly used ORMs like NHibernate and EF for the past few years. Typically in our web applications we implement session per request, beginning a transaction at the start of the request and…
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
15
votes
6 answers

Arithmetic overflow exception when opening SQL connection

I got very weird ArithmeticOverflowException when opening an SQL connection to the underlying SQL database (stack trace included below). It doesn't make a difference which version of the server is used (I've verified MS SQL 2005/2008/2012/2014),…
Red Developer
  • 151
  • 1
  • 4
14
votes
1 answer

NullReferenceException inside .NET code of SqlConnection.CacheConnectionStringProperties()

I'm facing really strange issue. Given the code below: static void Main() { var c = new System.Data.SqlClient.SqlConnection(); c.ConnectionString = "Data Source=SOME_NAME;Initial Catalog=SOME_DB;Integrated Security=True"; …
Pavel K
  • 3,541
  • 2
  • 29
  • 44
14
votes
7 answers

Proper way to deal with database connectivity issue

I getting below error on trying to connect with the database : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is…
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
14
votes
1 answer

Will SqlConnection get disposed by GC?

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…
Nelson Rothermel
  • 9,436
  • 8
  • 62
  • 81
13
votes
2 answers

When is DbConnection.StateChange called?

I have the following code: class Program { static void Main() { var connection = new SqlConnection("myConnectionString"); connection.Open(); connection.StateChange += HandleSqlConnectionDrop; …
Jacob Horbulyk
  • 2,366
  • 5
  • 22
  • 34
13
votes
8 answers

How to write connection string in web.config file and read from it?

I'm trying to write Connection string to Web.config like this: and read from it…
user2715779
  • 147
  • 1
  • 1
  • 4
12
votes
3 answers

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

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,…
user166390
12
votes
2 answers

Difference between Sql Connection and OLEDB Connection

What is the difference between the SQL Connection and OLEDB Connection? Is that OLEDB is common to all (also SQL Server)? To which are all Servers, OLEDB is using?
Abdul Rahman
  • 682
  • 2
  • 6
  • 20
12
votes
3 answers

how many instances of SqlConnection should I use

Background: I have an application that I have nicely separated my interface logic from my middle tier logic which handles the queries to the database. I do a lot of custom sorting and narrowing so I'm not using many SqlDataSources and instead…
Justin C
  • 1,924
  • 4
  • 28
  • 43
12
votes
2 answers

Most efficient way to test SQL connection string availibility

I have this code down which I tried to make it Test SQL string connectivity, but I dont know how to handle the part with connection.Open = true would you please help me to solve this out? Thank you so much for your time. private void…
Marek
  • 3,555
  • 17
  • 74
  • 123
11
votes
2 answers

Does SqlDataAdapter close the SqlConnection after Fill() function?

Does SqlDataAdapter close the SqlConnection after the Fill() function or do I need close it myself? string cnStr = @"Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm"; cn = new…
Wachburn
  • 2,842
  • 5
  • 36
  • 59
10
votes
1 answer

SQLConnection pool size and active connection count in C#

In C# it is possible to enable/disable Connection Pooling by using "Pooling=True" and "Max Pool Size=XY" in connection string. Like: What is maximum allowable value of "Max Pool Size" in sql connection string Is it possible to ask how many…
phoad
  • 1,801
  • 2
  • 20
  • 31
9
votes
4 answers

Isolation level in Sql Transaction

I have implemented SqlTransaction in c# to begin, commit and rollback transaction. Everything is going right, but I've got some problem while accessing those tables which are in connection during transaction. I was not able to read table during the…
user1082916
9
votes
3 answers

Is a SqlConnection automatically closed when an application is closed?

I am querying the db in a separate thread. If I close the application while the query is being executed, will the SqlConnection automatically close or will it remain open?
user143887
1 2
3
72 73