Retrieiving data from a database without having to put the file directory (REQ)
Hello
So basically I'm using a database to store all my data but whenever I access the database I have to put the file directory (see code below). This is going to be a problem as this is a college project, and my teacher/exam board will be trying out the system and because of each computer having different disk drive letters it will not work on other computers. I'm just wondering if there is a way for my to save the database in the project folder and the project to detect where it is no matter the file directory.
Thanks to anyone who can help :)
Code i'm using currently to add to a file -
Dim strSQL As String
Dim objCmd As New OleDbCommand
strSQL = "INSERT INTO tblUsers(FirstName,Surname, userName, userPassword, userPosition) "
strSQL = strSQL & "VALUES('" & (Firstname) & "', '" & (Surname) & "','" & (Username) & "', '" & (Password) & "', '" & (Position) & "');"
Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=FILEDIRECTORY")
Dim myConnection As OleDbConnection = New OleDbConnection
Con.Open()
objCmd = New OleDbCommand(strSQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()
Re: Retrieiving data from a database without having to put the file directory (REQ)
You have a few choices the easiest is if your database is always going to be in the same directory as your application you can do this -
- You will need to import both - System.Reflection & System.IO
Code:
Dim location = Assembly.GetExecutingAssembly().Location
Dim appPath = Path.GetDirectoryName(location)
This will give you the path of your application!
Re: Retrieiving data from a database without having to put the file directory (REQ)
Ahh thank you very much, all I had to do from there was to add the database file name which won't change to the string from the location, your a life saver :)
Re: Retrieiving data from a database without having to put the file directory (REQ)
no problem :) remember to mark your thread as resolved