ADO.NET SQL Server Connection

Microsoft .NET Framework Data Provider for SQL Server proves to be a valuable tool. The initial step in connecting the application to the database involves creating an instance of the Server object and establishing a connection with the SQL Server instance.

SqlConnection Object

In VB.Net, the SqlConnection Object serves as the primary component responsible for handling the physical communication between the application and the SQL Server database. This object is part of the .NET Framework's Data Provider for SQL Server Database, providing the necessary functionality to establish, manage, and interact with the database.

To create an instance of the SqlConnection class in the .NET Framework, developers need to provide a connection string as an argument, which contains essential information such as the server name, database name, credentials, and any additional settings required for the connection. This connection string is passed to the constructor of the SqlConnection class, enabling the creation of the SqlConnection instance.

Once the SqlConnection instance is established and the connection is successfully made to the SQL Server database, developers can utilize SQL commands to execute queries, updates, or stored procedures, using the capabilities of the Connection Object. These SQL commands allow for data retrieval, manipulation, or any other required operations within the database.

Sql Server connection string

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName; User ID=UserName;Password=Password"

If you have a named instance of SQL Server, you'll need to add that as well.

"Server=localhost\sqlexpress"

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.

A Sample VB.Net Program that connect SQL Server using connection string.

Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim cnn As SqlConnection connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" cnn = New SqlConnection(connetionString) Try cnn.Open() MsgBox("Connection Open ! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

Connecting to SQL Server using windows authentication

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

Trusted Connection from a CE device

connetionString="Data Source=ServerName; Initial Catalog=DatabaseName;Integrated Security=SSPI; User ID=myDomain\UserName;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=DatabaseName; User ID=UserName;Password=Password"

1433 is the default port for SQL Server.

A sample VB.Net program that demonstrate how to execute sql statement and read data from SQL server.

Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As SqlConnection Dim command As SqlCommand Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here , like Select * from product" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(sql, connection) Dim sqlReader As SqlDataReader = command.ExecuteReader() While sqlReader.Read() MsgBox(sqlReader.Item(0) & " - " & sqlReader.Item(1) & " - " & sqlReader.Item(2)) End While sqlReader.Close() command.Dispose() connection.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

A sample VB.Net program that perform Data Manipulation tasks like Insert , Update , Delete etc. also perform by the ExecuteNonQuery() of SqlCommand Object.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As SqlConnection Dim command As SqlCommand Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(Sql, connection) command.ExecuteNonQuery() command.Dispose() connection.Close() MsgBox(" ExecuteNonQuery in SqlCommand executed !!") Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class