Connection Object in ADO.Net

The Connection object in ADO.Net is used to connect your application to a specific data source by providing the required authentication information in connection string. The connection object is used according to the type of the data source.
The data provider contains the Connection, Command, DataReader, and Data Adapter objects. The Connection object provides connectivity to the database. The Command object provides access to database commands to retrieve and manipulate data in a database.
Sql Server connection string


connetionString = "Data Source=Server Name;Initial Catalog=Database Name;User ID=User Name;Password=Password"

If you have a named instance of SQL Server, you’ll need to add that as well.
Open() is used to open SqlConnection. The Close() method in SqlConnection class is used to close the Database Connection. The Close method rolls back any pending transactions and releases the Connection from the SQL Server Database.

Connecting to SQL Server using windows authentication


"Server= localhost; Database= Database Name;Integrated Security=SSPI;"

Trusted Connection from a CE device


connetionString="Data Source=Server Name;Initial Catalog=Database Name;Integrated Security=SSPI; User ID=myDomain\User Name; Password=Password;"

This will only work on a CE device

Connect via an IP address


connetionString="Data Source=IP_ADDRESS,PORT; Network Library=DBMSSOCN; Initial Catalog=Database Name; User ID=User Name; Password=Password"

1433 is the default port for SQL Server.



Demo Code