What is proper way to define connection string VB - vb.net

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.

Related

Unable to open shared SQLite database

Unable to open database file
That's the error I get when trying to open a SQLite db file that is shared, I've already allowed read/write to everyone when I shared the folder containing the db file.
I'm accessing the file using LINQPad or SQL Server Compact & SQLite Toolbox both gives me the same error.
However I can successfully work with the database using my Windows Form application with the following declaration for the connection
Dim litecon As SQLiteConnection = New SQLiteConnection("Data Source=\\10.10.10.10\SQLite\SQLiteDB.db;Version=3;Password=1;", True)
I set parseViaFramework to True

connection string path dynamically in 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.

How can I get access to the files root directory without giving full path name in VB .NET

I am developing a Windows application in Visual Studio 2008 with SQL Server CE as database. D:/My Project/Library is the root directory for my project's source code.
I have some files which I want to access within the forms of my project. They are stored in D:/My Project/Library/New Resources. To access those files I can't use the full path name because I keep changing the location of my project's source code. I need something like wherever the source directory may be the files must be accessed from there only.
Ex:
Public Sub setconnection()
con = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=H:\Library\Library\Library1.mdf;Integrated Security=True;User Instance=True")
cmd.Connection = con
con.Open()
End Sub
I want to use relative path name for Library1.mdf file without using H:\Library\Library path name.
This has worked for me, though you may need to tack on some additional info. A web project using this, for example, may need to append "/bin" to the end of the result to get the actual location of the DLLs.
AppDomain.CurrentDomain.BaseDirectory

I want my vb.net program with sqldb to be transferable

I'm having trouble with my vb.net program. It has a database with a SqlConnection string of:
DbConn = New SqlConnection("Data Source=ACE-DUO;Initial Catalog=db_CVSO;Persist Security Info=True;User ID=sa;Password=pwd")
I made an installer for this vb.net program but I'm having problems regarding my SQL Server connection string. It's because once I installed the program in different computer. The server name in my case (ACE-DUO) changes and the database itself cannot be located.
I know how to detach the file and attach it to vb.net program. what I'm really aiming for is that I want the connection string to change on based where the program resources were placed.
For example, if the program was installed in the C:\Program Files\MyDatabase folder, I want to make it as a part of the connection string so it would be opened in different computer.
If you don't need multiple shared access to your database you could take advandage of the LocalDB feature of Sql Server 2012.
You connection string could be changed to
DbConn = New SqlConnection("Server=(localdb)\v11.0;Integrated " & _
"Security=true;AttachDbFileName=C:\Program Files\MyDatabase\db.mdf;"
Article on LocalDB

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.