Yes and no,
What I've done in the past, is place the MDB as a resource inside the EXE and "copy it down" and save it on disk if the db is not found at a specific folder. Reading resources is fairly easy and standard and writing it out to a file is as well. The name of the resource is typically prefixed with the namespace (in the example below, the namespace was MFV.Database and the filename/resource was blank.mdb.
Dim objStream As Stream
Dim objStreamOutput As FileStream
If not File.Exists(strFileName) Then
objStream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("MFV.Database.Blank.mdb")
objStreamOutput = New FileStream(strFileName, FileMode.CreateNew)
Do While objStream.Position < objStream.Length
objStreamOutput.WriteByte(CByte(objStream.ReadByte))
Loop
objStreamOutput.Close()
objStream.Close()
End If
As for the specified folder to "extract it to", I would recommend using the application data folder, typically <local app data>\<company>\<product>\<version>. I would recommend using that folder as the current user always have read/write access to that folder (guaranteed).
You can fetch the local application data folder using Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) and as for the rest, simply concatenate the company name, the product and the version seperated by \.
Cheers
Nic
I would suggest the following simple procedures.
1) Place your database file (mdb) in the application directory (bin or release depending on debug version).
2) Right click on your project in the Solution Explorer and choose add Existing Item. Locate your .mdb file and add it.
3) Change your connectionString to refere to your app directory . You can get your application directory by
My.Application.Info.DirectoryPath
Hi,
To add a database as a resource (or any type of file), follow these steps:
1) File \ Add existing item
2) Select your file (in this case, a DB file). It should now appear in the solution explorer panel.
3) Select it and use right-click properties. The properties panel should appear
4) Under the Build Action property, select Embedded Resource
The file will now be "packaged" inside of the project it was added to (exe or dll).
Cheers
Nic
Nicolas_qc_au wrote:
Yes and no,
What I've done in the past, is place the MDB as a resource inside the EXE and "copy it down" and save it on disk if the db is not found at a specific folder. Reading resources is fairly easy and standard and writing it out to a file is as well. The name of the resource is typically prefixed with the namespace (in the example below, the namespace was MFV.Database and the filename/resource was blank.mdb.
Code Snippet
Dim objStream As Stream
Dim objStreamOutput As FileStreamIf not File.Exists(strFileName) Then
objStream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("MFV.Database.Blank.mdb")objStreamOutput = New FileStream(strFileName, FileMode.CreateNew)
Do While objStream.Position < objStream.Length
objStreamOutput.WriteByte(CByte(objStream.ReadByte))
Loop
objStreamOutput.Close()
objStream.Close()End If
As for the specified folder to "extract it to", I would recommend using the application data folder, typically <local app data>\<company>\<product>\<version>. I would recommend using that folder as the current user always have read/write access to that folder (guaranteed).
You can fetch the local application data folder using Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) and as for the rest, simply concatenate the company name, the product and the version seperated by \.
Cheers
Nic
Hi,
Sorry about this, the Stream class is part of System.IO namespace
You may need to either put an "Imports System.IO" statement at the top of the file or fully qualify the name as in "System.IO.Stream" and "System.IO.FileStream"
Cheers
Nic
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Hi,
When it's building or when it's running.
I am not sure why it is complaining. If "CommonApplicatonData" does not work, try "LocalApplicationData". Alternatively, look at the return values when using either of those. They should look like "C:\Users\All Users\Application Data\" and "C:\Users\username\Application Data\" respectively.
Cheers
Nic
objStreamOutput = New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FileMode.CreateNew)
Hi,
First off, note that you did not append a file name in the original "syntax" which may have been the problem you were having.
As for extracting it to the current folder, you need to change the "destinationm" of the output stream.
Replace the following line
with
objStreamOutput = New FileStream(System.IO.Directory.GetCurrentDirectory() & "\" & "filename.mdb", FileMode.CreateNew)
Cheers
Nic
Hi,
It would depend how your application is launched.
If you are using a "sub main", then you would place the code at the end of the sub.
If you are using a "form", then you would place the code in the close event of the form.
Either way, the code would be
System.IO.File.Delete(strFileName)
Where strFileName is the same parameter as used to output to the mdb file.
Cheers
Nic