SqlConnection Class
Namespace:
Assembly:
System.Data.dll
Inheritance
Object → Component → SqlConnection
Remarks
The SqlConnection class represents a unique session to a SQL Server data source. This class is used to configure and execute SQL statements and commands against the SQL Server database.
This class is inherited from DbConnection.
The SqlConnection object can be created using the default constructor or by passing a connection string as a parameter.
SqlConnection connection = new SqlConnection("Data Source=server;Initial Catalog=database;User ID=user;Password=password;");
Constructors
SqlConnection()
SqlConnection(string connectionString)
The connection string specifies the properties required to connect to the data source, such as the server name, database name, and authentication credentials.
Properties
ConnectionString
ConnectionTimeout
Database
ServerVersion
Methods
Open()
If the connection is already open, this method does nothing. It is recommended to check the State property before calling Open.
Close()
This method releases the connection resources. It is important to close connections when they are no longer needed to prevent resource leaks.
CreateCommand()
The returned command can be configured with SQL text and parameters to be executed against the data source.