Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm still a newbie so please pardon my question. I just want to know how to avoid hard coding the connection string in every sub in vb.net. I'm using vb.net 2017 and access as my database. I have multiple connections to my database and I want to know if there is an efficient way to this?

Below is my connection string and I put it in a module so every form that I have made can access it and I just need to call the "conSTR" variable.

What I have tried:

Public conSTR As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VBactivity\PAGEANT\PAGEANT.accdb"


and below is how I used the conSTR variable

Using cons As New OleDb.OleDbConnection(conSTR)

    Using cmd As New OleDb.OleDbCommand


        With cmd
            .CommandText = "INSERT INTO Category (CategoryName, CatPercent) VALUES (@Category ,@CatPercent)" ', @CatPercent)"
            .Parameters.AddWithValue("@Category", txtCategory.Text)
            .Parameters.AddWithValue("@CatPercent", numCategory.Value)
            .Connection = cons
            cons.Open()
            cmd.ExecuteNonQuery()
            cons.Close()
            MessageBox.Show("CATEGORY HAS BEEN ADDED", "CATEGORY", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End With


    End Using


End Using
Posted
Updated 2-Apr-20 18:06pm
Comments
Maciej Los 30-Mar-20 1:35am    
If conSTR is a variable of public scope, you don't need to pass it into procedure. See:
Scope - Visual Basic | Microsoft Docs[^]
How to: Control the Scope of a Variable - Visual Basic | Microsoft Docs[^]
You can define connection string in ap.config file. See: Connection Strings and Configuration Files - ADO.NET | Microsoft Docs[^]
Tomas Takac 30-Mar-20 1:56am    
lelouch_vi 2 4-Apr-20 3:22am    
Thank you for pointing that out. Now I realize it already why hard coding connection string is a bad idea. I already have my connection string to my app.config.

1 solution

VB
'Create a Module and Create Fuction Connect 
' You can connect From database local or network area 
Public Sub connect()
Public Con As New OleDbConnection
        M_drivepath = Left(Application.StartupPath, 1)
        If My.Computer.Name.ToString() = "MSERVER" Then
            Con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\WIN_SOFT\RETAIL_SOFT\Datafile\DATA" & aWork_Data & ".accdb;Jet OLEDB:Database Password=M1423456"
        Else
            Con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\WIN_SOFT\RETAIL_SOFT\Datafile\DATA" & aWork_Data & ".accdb;Jet OLEDB:Database Password=M1423456""
        End If
       
      
        Brn_File = "SDBRN14"    ' SDBRN14 IS TABLE NAME STORE IN Brn_File
    Con.Open()
    End Sub
' Where you want to Connect Database write Connect 
' else Check Connection Open or not
' Write Command   If Con.State = ConnectionState.Closed Then Con.Open()
 
Share this answer
 
v2
Comments
lelouch_vi 2 4-Apr-20 3:21am    
Thank you guys for pointing additional knowledge. I already solve it. I put my connection string inside the app.config since I'm working with windows application. Now I fully understand why hard coding connection string to every class or event is a very bad idea.

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