Net-informations.com
SiteMap  | About    

ADO.NET SQL Server Connection



VB.Net SQL Server Connection

You can connect your VB.Net application to data in a SQL Server database using the Microsoft .NET Framework Data Provider for SQL Server. The first step in a VB.Net application is to create an instance of the Server object and to establish its connection to an instance of SQL Server.

ADO.NET SQL Server Connection

The SqlConnection Object is Handling the part of physical communication between the application and the SQL Server Database. An instance of the SqlConnection class in .NET Framework is supported the Data Provider for SQL Server Database. The SqlConnection instance takes Connection String as argument and pass the value to the Constructor statement. When the connection is established , SQL Commands may be executed, with the help of the Connection Object, to retrieve or manipulate data in the database. Once the Database activities over , Connection should be closed and release the database resources .

Sql Server connection string

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

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.

Connecting to SQL Server using windows authentication

Trusted Connection from a CE device

This will only work on a CE device

Connect via an IP address

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.

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





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





net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.