connection string path dynamically in vb.net - vb.net

I made a desktop application in vb.net. I give the reference path of connection string through the .udl. The problem is when I place the folder in another location the path string does not change and hence results in an error. Kindly suggest. Connection details are as follows
_connStr = System.IO.File.ReadAllText("C:\Users\avt\Desktop\New folder\a.udl")
con = New OleDbConnection(_connStr)

First of all, why are you using Text file to save your connection String ?
Go to Project Properties -> Settings and create a ConnectionString setting to save your connection string. That'll solve your problem. Using a Text file for ConnectionString is not a valid way.

Related

Setting MS Access password at runtime in vb.net designer generated system

I am developing a VB.NET update system for a volunteer organisation’s MS Access database. The database is protected by a password as it contains personal information. I have created the application using the VB designer. I need to be able to code the application so that, if the owner decides to change the MS Access password, they will have no need to come back to me to change the code and rebuild the solution. In other words, I do not want the password to be hard coded in the app.config file or the settings.designer.vb file. My code should not need to know the password as a simple call to one of the Fill functions can test any password entered by the user. My problem is that I have found no way to alter the connection string that is tested in the setttings.designer.vb code whenever the database is accessed. I am using Visual Studio 2017.
I have spent a long time searching the web for answers and have tried various solutions involving the configurationmanager without success. I am new to this area so I would be most grateful if anyone here can help.
Here is my latest attempt which still produces an invalid password error even though the third debug statement suggests that the connection string, including the password, has been correctly set.
Public Sub UpdateConnString(connString As String)
Dim configFileMap As New ExeConfigurationFileMap()
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(configFileMap.ExeConfigFilename)
Dim connStringName As String = "TestConnectionString"
Debug.Print("0 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString = connString
Debug.Print("1 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
config.Save(ConfigurationSaveMode.Modified, True)
Debug.Print("2 " + config.ConnectionStrings.ConnectionStrings(connStringName).ConnectionString)
End Sub
Just because a connection string is stored in the config file, you aren't required to use it as it is. You can read in that default value and then edit it before using it, e.g.
Dim builder As New OleDbConnectionStringBuilder(My.Settings.DefaultConnectionString)
builder.DataSource = dataSource
Dim connectionString = builder.ConnectionString
You can add or modify any part of a connection string you want that way at run time.
Thank you for your response. Unfortunately, the code throws a compilation error - "DefaultConnectionString is not a member of My.Settings".
Fortunatley I have now managed to find a working solution:
'My.Settings.Item("TestConnectionString") = connectionString

VB.NET Declaring new SQL Connection

I have a public class within a module and there is a variable which holds my connection string like this:
Public ConnectionS As New SqlConnection("Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=\MyDB.mdf;Integrated Security=True")
The code above works and saves my Connection string correctly. However when I save my connection string to My.Settings.ConnectionS and use the following code:
Public ConnectionS As New SqlConnection(My.Settings.ConnectionS)
It doesn't save the connection string. When I use the first code above and output the value of ConnectionS into a textbox I get the exact connection string above but when I output the value of the second code I get nothing (blank textbox). I don't understand why because I have my connection string saved to My.Settings.ConnectionS in my application settings.
If I'm understanding your question correctly, you aren't saving the connection the correct way. Instead of saving the setting, you are instantiating a new setting but never actually saving that value to the user setting. The correct way to save your connection to My.Settings would be as follows:
My.Settings.ConnectionS = New SqlConnection("Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=\MyDB.mdf;Integrated Security=True")
My.Settings.Save()
Also, be sure that you've set up the ConnectionS setting with a type of SqlConnection.
Here is more information on saving user settings if you need it: https://msdn.microsoft.com/en-us/library/fwc80dzb.aspx

What is proper way to define connection string VB

My Visual Basic project uses this connecton string:
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Desktop\IzolacCold V2\IzolacCold V2\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
When I debug it, and go to debug folder, or when I "Release" it then problem occurs. If I run exe file program works normally but it save's data in database defined in connection string and not in database that is created in DEBUG folder (or RELESE folder).
How to properly connect to my databes. How to build proper connection string ?
You should use the DataDirectory substitution string.
For example you could write your connectionstring as
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\izolac_cold_dbv2.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
In your code then you could control the exact location of the DataDirectory executing this code, BEFORE any attempt to open a connection.
Dim commonDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim myAppDataFolder = Path.Combine(commonDataFolder, "MyAppName")
if Not Directory.Exists(myAppDataFolder) Directory.CreateDirectory(myAppDataFolder)
AppDomain.CurrentDomain.SetData("DataDirectory", myAppDataFolder)
(This example use the C:\programdata\myAppName folder because it is the most appropriate location for read/write data files needed by your application
Your deployment procedure should take care to create the above folder and place the database file in this location.

how to read the file which is in application itself and how to add in setup?

I have a class library which reads the XML file.
I am using VS 2012 and VB.NET language.
I am getting confused about how to read the file which is in folder of a application itself.
Right now I have given the path as
Dim reader As XmlTextReader = New XmlTextReader("C:\mailpara.xml")
but its hard-coded , but I want to make a folder in app. and want to read from that
folder itself.
I want to know how to read the file from the folder of a application.
How to read the file after installation on client's machine and how to add the file while making the set up ?
Use something like;
Dim directory as String = My.Application.Info.DirectoryPath
Dim reader As XmlTextReader = New XmlTextReader(directory & "\MyFolderName\mailpara.xml")
You can use Application.StartupPath property to retrieve the startup path of the application.
Dim reader As XmlTextReader = New XmlTextReader(Application.StartupPath & "mailpara.xml")
You might want to add a check to ensure that the path ends with a \ (I think it may or may not be present depending on whether the path is a root folder or not).

Connection String for a local database across multiple users

How do you use the connection string on a local database in a way that would allow you to transfer the project folder from one computer to another, without having to modify the connection string?
Not like this
connection_String As String = "Data Source=C:\Users\Kyle\Desktop\CSCI_388_Group_Project\CSCI_388_Group_Project\CSCI_388_Group_Project\CSCI_388_Group_Project_Database.sdf"
More like this
connection_String As String = "Data Source=|DataDirectory|\CSCI_388_Group_Project_Database.sdf"
If I try to enter the second example into the connection field under the database properties I get an illegal character error.
The Environment class contains most of the paths you'll need.
You can call Environment.GetFolderPath to get a special directory. For example, to get the My Documents directory, call Environment.GetFolderPath(Environment.SpecialFolder.Personal).
You can browse all of the special folders here:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
In your example specifically, you'd use the following:
connection_String As String = _
"Data Source=" &
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"CSCI_388_Group_Project_Database.sdf" )
Alternative:
Is it possible to just ask the user? If it's on their desktop, they might move it.
If you decide to keep the sdf file in the same folder in which your exe would reside, then you can do like this.
Dim ExeFolder As String = AppDomain.CurrentDomain.BaseDirectory
Dim connection_String As String = "Data Source=" & ExeFolder & "CSCI_388_Group_Project_Database.sdf"
Hope it helps !
One simple way is to use an environment variable to specify your path to the folder.
Then in your VB program using the Environ function to get the environment variable settings and use string manipulation to build your connect string.
You will need to have a default in case the environment variable is not set.
However when you launch your application, you can do it within a script that will set the environment variable.
A second method is to specify the path name on the application command line so that when the application is launched, it will specify the path using the command line.
A third method is to have a Windows registry key that you use. Then, similar to the environment variable, you query the Windows Registry for the folder path, then build your connect string from that.
With this third method you can have your application installer insert the Registry key.
A fourth method is to have an .ini file that contains an entry for the folder path.