loading a database at startup

Dave3000

New member
Joined
Feb 12, 2009
Messages
1
Programming Experience
1-3
Hello everyone

I am working on a VB application that uses an access database. I have a program that must be able to make backups of a mdb database and be able to open another database as the datasource. Now what I want to do is this:

I have a toolstrip combobox on my form with this code on form load:

'gets the directory and sets the file types to search for.
Dim di As New IO.DirectoryInfo("C:\WIndows")
Dim diar1 As IO.FileInfo() = di.GetFiles("*.exe")
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
ToolStripComboBox1.Items.Add(dra)
Next

I want the *.exe to be *.mdb but the *.exe was just for testing. could you tell me what the code is to get a file in a sub directory of where this application is stored?

I have a button beside this called "Load DBase" and the code is this:

MsgBox("You are about to load a different version of the database. Would you like to save the current information on database you are working on, before you continue?", MsgBoxStyle.YesNoCancel, "Load Database")

what I want is when you click the load Dbase button, the message box appears, and if you click yes, it makes a backup of the current database in a subfolder of where the application is stored called "Database Backups" using the current date as the file name e.g. "dbase121207" or "dbase131207" etc..

Then it loads the filename in the toolstripCombobox1 as the database the data on the application gets the data from. If you click no, it bypasses the backup option and just loads it.

If no file is selected in the toolstripcombobox, a message will say "please select a file before you continue"

I also want a way of detecting if the file being loaded is not a database or if it has different or wrong things like tables/queries in it, so that the program I am working on won't be linked to a wrong datasource or crash.

Any ideas what I could do?
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I want the *.exe to be *.mdb but the *.exe was just for testing
Make the extension *.mdb" in the search pattern

. could you tell me what the code is to get a file in a sub directory of where this application is stored?
Don't specify a path (uses the current directory) or make use of the Application.ExecutablePath / Application.StartupPath properties

it makes a backup of the current database in a subfolder of where the application is stored called "Database Backups"
Use System.IO.Directory.Create and System.IO.File.Copy to achieve this

using the current date as the file name e.g. "dbase121207" or "dbase131207" etc..
Use String.Format("dbase{0:ddmmyy}", DateTime.Now) to achieve this


Then it loads the filename in the toolstripCombobox1 as the database the data on the application gets the data from.
I recommend you read the DW2 link in my signature to find out how to do data access. Then I recommend you add a property to the user customizable section of the My.Settings code so that you can write to the ConnectionString in the settings, and use this to globally change what DB will be used



I also want a way of detecting if the file being loaded is not a database or if it has different or wrong things like tables/queries in it, so that the program I am working on won't be linked to a wrong datasource or crash.

That's what try/catch blocks are for, but you can also run queries against all tables you need to be there, when connecting. If any crash, edit the db, or inform the user the database is unsuitable
 
Top Bottom