ARTICLE

How to use connection object in ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 10, 2011
Here we see how to use ado.net to connect to a database with the open and close method of SqlConnection class.
Download Files:
 
Reader Level:

Connection object

The connection object is used to open a connection to your database (data source). The SqlConnection instance takes Connection String as argument and pass the value to the Constructor statement. The Open() method in SqlConnection class is used to open the Database Connection and The Close() method in SqlConnection class is used to close the Database Connection.

Creating a SqlConnection Object

To create a connection object we pass the connection string.

Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

Dim con As New SqlConnection(str)

 

Open() method

 

The open method in SqlConnection class is used to open the Database Connection.

con.open()

 

Close() method

 

The close method is SqlConnection class is used to close the Database Connection.

con.close()

For example

The below example define the connection object with SQL Server database.

 

Imports System.Data.SqlClient

Module Module1

    Sub Main()

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        Try

            con.Open()

            Console.WriteLine("connection open")

            con.Close()

        Catch ex As Exception

            Console.WriteLine("can not open connection")

        End Try

    End Sub

End Module

OUTPUT

con1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor