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
9
votes
1 answer

Inject IConfiguration into Program.cs (Console App) in .NET Core 3.0

I am trying to inject IConfiguration (Microsoft.Extensions.Configuration package) into Program.cs and don't know if that is possible and therefore obviously don't know how to do it, if possible. I've done it in the Startup class in other projects,…
nelion
  • 1,712
  • 4
  • 17
  • 37
9
votes
1 answer

Why both SqlConnection and SqlTransaction are present in SqlCommand constructor?

I wonder, what is the reason to have this SqlCommand constructor overload: public SqlCommand( string cmdText, SqlConnection connection, SqlTransaction transaction ) ? When I need to create an internal method that does its bit using a…
GSerg
  • 76,472
  • 17
  • 159
  • 346
9
votes
2 answers

Database file not copied during publishing so installed application throws exception

I am developing a C# windows form application containing a service based data based. when I test my application it's database works fine but after publishing and installing the program when program tries to open sqlconnection , this error appears:…
Behnam
  • 1,039
  • 2
  • 14
  • 39
9
votes
1 answer

SqlConnection Thread-Safe?

I have a Log class which put logs in Windows journal and in a SQL table. In order to optimize my code, I would like use only one SqlConnection. In MSDN, it says: Any public static (Shared in Visual Basic) members of this type are thread safe. Any…
BaptX
  • 561
  • 1
  • 7
  • 21
9
votes
3 answers

ASP.NET use SqlConnection connect MySQL

This is the connection string saved in web.config: This is the code to connect to the…
John Walker
  • 1,121
  • 4
  • 26
  • 68
9
votes
1 answer

Managing SQLConnection / Datasnap through client-server disconnects

In my Datasnap client application I use 1 TSQLConnection for my methods and ProviderConnection. The problems arise when the connection is lost. Both TSQLConnection.Connected and TSQLConnection.ConnectionState don't catch this. When my TSQLconnection…
r_j
  • 1,348
  • 15
  • 35
8
votes
8 answers

Using C#'s 'using' statement with a custom object's function, do I Need to implement IDisposable?

I have an sqlConnection manager class like so: public class SQLConn { public string connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; private SqlConnection sqlConn; public SqlConnection Connection() { …
zulkamal
  • 578
  • 1
  • 4
  • 17
8
votes
3 answers

ExecuteReader requires an open and available Connection. The connection's current state is closed

Ok, I asked about this very error earlier this week and had some very helpful answers and without doubt things have drastically improved since I started following the suggestions. However, now I am using the 'correct', best practice method to access…
dooburt
  • 3,010
  • 10
  • 41
  • 59
8
votes
2 answers

Is there a standard .NET way to test if a SqlConnection string works?

Possible Duplicate: How to check if connection string is valid? Currently I'm doing it like this: internal bool CheckConnection() { using (SqlConnection testConn = new SqlConnection(this.ConnectionString)) { try { …
Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
8
votes
1 answer

Does SqlConnection processes queries in parallel?

If I open a SqlConnection to a SQL Server, and then issue multiple queries from multiple background threads, all using that one connection - will those queries be executed sequentially (don't care about the order)? Specifically, if at the beginning…
THX-1138
  • 21,316
  • 26
  • 96
  • 160
8
votes
7 answers

Test sql connection without throwing exception

To test if i can connect to my database, I execute the following code : using (SqlConnection connection = new SqlConnection(myConnectionString)) { try { connection.Open(); canConnect = true; } catch (SqlException) { } } This…
Alexandre Pepin
  • 1,816
  • 3
  • 20
  • 36
8
votes
6 answers

What disadvantages are there for leaving an SQL Connection open?

This seems to be a simple question, but I wonder the disadvantages of not calling the "close()" function.
stckvrflw
  • 1,489
  • 3
  • 22
  • 37
8
votes
1 answer

What are those curious properties that I've found in my sqlConnection?

Whilst investigating a very dull and exponetially security flawful 2005 project, I debugged into a connection hang. Inspecting the object, to find the server name, I've encountered this three little properties: IsShiloh…
marcelo-ferraz
  • 3,147
  • 4
  • 38
  • 55
8
votes
1 answer

Should you reuse SqlConnection, SqlDataAdapter, and SqlCommand objects?

I'm working with a DAL object that is written in a layout similar to the following code. I simplified a lot of the code code just to show the setup. public class UserDatabase : IDisposable { private SqlDataAdapter UserDbAdapter; private…
Equixor
  • 259
  • 4
  • 13
7
votes
4 answers

Managing SQL Server Connections

What is the the best practice for SQL connections? Currently I am using the following: using (SqlConnection sqlConn = new SqlConnection(CONNECTIONSTRING)) { sqlConn.Open(); // DB CODE GOES HERE } I have read that this is a very effective…
Neale
  • 463
  • 6
  • 17