application.startuppath database access

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
ok this one is kind of interesting. i have an application that access an msaccess (jetdriver 4.5 or whatever it is) that'll always be in the app's start up directory. now the problem is that this application wont always be in the same directory on all of the computers.

is there a way to have the vb app modify it's connection string to the database in a application.startuppath & "\buildputer.mbd" type way on startup?
 

suddenelfilio

Well-known member
Joined
Jun 3, 2004
Messages
144
Location
Belgium
Programming Experience
5-10
try this

Use the System.Reflection.Assembly.GetExecutingAssembly.Location property. This is a shared property so you don't have to create an instance. This will return the complete path (including filename) of the application.
 

ayan

Member
Joined
Jun 9, 2004
Messages
18
Programming Experience
Beginner
put your db in your_project_folder\bin. that way, you can use application.startup & "\db.mdb" or something
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
ayan said:
put your db in your_project_folder\bin. that way, you can use application.startup & "\db.mdb" or something
lol, yes i've tried that, but the connection string property doesnt recognize application.startuppath as a command. and when i try to modify the connection string in code it doesnt work cause there are ' in the connection string which simply remarks the rest of it out... i can do this type of thing jes fine in vb6, but .net is gay when connecting to databases
 

ayan

Member
Joined
Jun 9, 2004
Messages
18
Programming Experience
Beginner
like this?
VB.NET:
 	  Dim cn As New OleDbConnection()
 	  cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\db.mdb"
 	  cn.Open()
i wonder your computer doesn't recognize application.startuppath. are u using console apps??? if so, try environment class...
 

staceypooh

New member
Joined
Jul 1, 2004
Messages
2
Programming Experience
1-3
ayan said:
like this?
VB.NET:
 	  Dim cn As New OleDbConnection()
 	  cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\db.mdb"
 	  cn.Open()
i wonder your computer doesn't recognize application.startuppath. are u using console apps??? if so, try environment class...
HI, I am having the same problem connecting to my access database. Here is my connection string:

Me.OleDbConnection2.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Data Source=" & Application.StartupPath & "\Network_Mapping_Tool_v2.mdb"";Jet OLEDB:Engine Type=5;Pro" & _
"vider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;p" & _
"ersist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:E" & _
"ncrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Cop" & _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID" & _
"=Admin;Jet OLEDB:Global Bulk Transactions=1"

When I try to compile, the error reads:
"Format of the intialization string does not conform to specification starting at index 215"
Can you please help? I am new to .NET, and miss my app.path :(
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
In the future, try using the code tags. See this link for help: vB Code List
Your Connection string would look like this:
VB.NET:
Me.OleDbConnection2.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=" & Application.StartupPath & "\Network_Mapping_Tool_v2.mdb"";Jet OLEDB:Engine Type=5;Pro" & _
"vider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;p" & _
"ersist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:E" & _
"ncrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Cop" & _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID" & _
"=Admin;Jet OLEDB:Global Bulk Transactions=1"

There are several Problems I see. Any setting without a value usually gets an empty string (four quotes inside of quotes, i.e. """"). The Provider setting shouldn't be enclosed in quotes.

This may work:
VB.NET:
Me.OleDbConnection2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
"Password="""";User ID=Admin;" & _ 
"Data Source=" & Application.StartupPath & "\Network_Mapping_Tool_v2.mdb;" & _
"Mode=Share Deny None;Extended Properties="""";" & _ 
"Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";" & _ 
"Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;" & _ 
"Jet OLEDB:Database Locking Mode=1;" & _ 
"Jet OLEDB:Global Partial Bulk Ops=2;" & _ 
"Jet OLEDB:Global Bulk Transactions=1;" & _ 
"Jet OLEDB:New Database Password="""";" & _ 
"Jet OLEDB:Create System Database=False;" & _ 
"Jet OLEDB:Encrypt Database=False;" & _ 
"Jet OLEDB:Don't Copy Locale on Compact=False;" & _ 
"Jet OLEDB:Compact Without Replica Repair=False;" & _ 
"Jet OLEDB:SFP=False"

You may want to change some settings to suit your needs. :)
 
Last edited:

bloukewer

Well-known member
Joined
Jul 2, 2004
Messages
88
Programming Experience
3-5
"Simple" solution

I have a solution, which might not be the best, but it worked fine for me.

Let's say you created a database called 'db.mdb'. Put it in the bin-directory of your project. Secondly, create all your connections and data-adapters and stuff as usual (no funny stuff). Then do a search on the name of your connection, including hidden code, until you find the connection's connectionstring. Copy this and paste it to the form's load event-handler. Now look for the bit that contains the path, and modify it using application.startuppath & "/db.mdb".

Now you can copy the program into any directory and it will work. Just keep the project in the same directory.

I hope this makes sense to you, but it's really not that difficult once you've done it. (just a bit tedious. writing a procedure with parameters could ease the tediousness a quite a bit)
 

bloukewer

Well-known member
Joined
Jul 2, 2004
Messages
88
Programming Experience
3-5
bloukewer said:
I have a solution, which might not be the best, but it worked fine for me.

Let's say you created a database called 'db.mdb'. Put it in the bin-directory of your project. Secondly, create all your connections and data-adapters and stuff as usual (no funny stuff). Then do a search on the name of your connection, including hidden code, until you find the connection's connectionstring. Copy this and paste it to the form's load event-handler. Now look for the bit that contains the path, and modify it using application.startuppath & "/db.mdb".

Now you can copy the program into any directory and it will work. Just keep the project in the same directory.

I hope this makes sense to you, but it's really not that difficult once you've done it. (just a bit tedious. writing a procedure with parameters could ease the tediousness a quite a bit)

Just to elaborate... You must change the connectionstring that you pasted onto the form's load event-handler, not the other one.
 
Top Bottom