A Connection object in ADO.NET is used to establish a connection between a program (the program may be a windows page, a web page, a windows service, a web service etc.) and the database. The connection is open just long enough to get or update data. By quickly opening, then closing a connection, the server resources are used as little as possible. See code below on how to open a connection between UI and database...
'Code below in VB.NET ...
Dim objectConn as SqlClient.SqlConnection
Dim strConn as String
Try
'First, create a connection object
objectConn=New SqlClient.SqlConnection()
'Next, build the Connection String
strConn &="Data Source=(local
strConn &="Initial Catalog=DatabaseName;"
strConn &= "User ID=admin;"
strConn &= "Password=;"
'Note here that the connection string may also be passed as a parameter
'to the connection string object during instantiation
objectConn.ConnectionString = strConn
objectConn.Open() 'Open the Connection
'The connection is now open
'Write your vb.net code here for operations on the database
objectConn.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
CONNECTION STRINGS FOR ACCESS, SQL 2000, SQL 2005, Oracle, MySQL
What are Connection Strings - A connection string has a group of semi-colon-separated attributes. Every .Net Data Provider connection string looks different, depending on the type of .NET Data Provider you need to use and which attributes are set for each different type of database system. An example, the connection string below is an example of what you use to connect to a local SQL Server. See that every parameter is separated by a semicolon.
Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=;
The connection string shown below is an example of how to connect to a Microsoft Access 2000 database using the OleDbConnection object in System.Data.OleDb.
Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\Northwind.mdb
Parameters in a Connection String - The parameters depend on the data provider is being used.
Server - The name of the SQL Server to which connection has to be established through ADO.NET. This is the name of the system that is running SQL server. We may use "local" or "localhost" for local computer. In case we are using named instances of SQL server, then the parameter would contain the computer name, followed by a backslash, followed by a named instance of the SQL server.
Database - The name of the database to which connection is to be established.
User ID - A user ID configured in the SQL Server by the SQL Server administrator.
Password - As the attribute name suggests, this is the password associated with the user id.
Note that connection string may also contain the Windows NT account security settings. This is done by passing the paramater "integrated security=true".
Labels: ADO.NET