3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by poojavb
0

Create a module in your vb.net project....

create functions for open connection and close connection....

In open connection give the database complete connection and in close connection just close the connection object.....

To call the connections in the form u can call the functions respectively....

find the code below its for MS SQL connection.....

Imports System.Data.SqlClient
Imports System.IO

Module DBConnection
    Public Connection As SqlConnection = New SqlConnection()

    'Open Connection
    Public Function Open_DB_Connection() As String
        Try
            Connection.ConnectionString = "Data Source = LocalHost\;Initial Catalog = Databasename;Integrated Security  = True"
            Connection.Open()
            'Debug.Print("Database connection opened successfully")
            Return "Success"
        Catch ex As Exception
            Return "Fail"
            Exit Function
        End Try
    End Function

    'CLosing Connection
    Public Function Close_DB_Connection() As String
        Try
            Connection.Close()
            'Debug.Print("Database connection closed successfully")
            Return "Success"
        Catch ex As Exception
            Return "Fail"
            Exit Function
        End Try
    End Function

End Module

For calling function on other forms. Suppose on a button click event

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Open_DB_Connection()
        'ur activity over here like queries
    Close_DB_Connection()

 End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.