please how can i use modles to drive my database in VB2010

Recommended Answers

Here's an article on managing a database in.net. the code is in C# but it shouldbe relatively easy to understand.

Jump to Post

All 2 Replies

Here's an article on managing a database in.net. the code is in C# but it shouldbe relatively easy to understand.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.