Click here to Skip to main content
14,984,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Please let me know how can i make sql connection in vb class so that at every page i dont have to make sql connection, connection can become by using object of class
What have you tried so far?

Put the connection string in your web.config file. Then create a class that contains methods to retrieve the connection string and return it.

    <add name="My_ConnectionString" providername="" connectionstring="Server=servername;Database=databasename;Uid=userid;Pwd=password;" />

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.IO

Namespace YourNameSpace
    Public Class ConnectionString
          Public Function GetConnectionString() As String

    Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)

    Return DBConnection
End Function
    END Class
END Namespace

This is a very basic solution to give you an idea. You may have to adjust it in order to get what you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900