kidwizz

Hey, I have created a basic database project and i want to distribute the file as a single .exe but it needs the .mdb to be in the same directory. Is there a way to include the. mdb inside the .exe file


Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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 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





Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

thanks but how to i make the database resource. (i'm new to VB)




Re: Visual Basic Language Include .mdb file in the .exe

Aw Ali

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






Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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





Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

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 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




This does not work. I am using VB Express 2005 and it says that Stream is not defined.




Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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





Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

thanks its almost working except when i use

Code Snippet

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)


When its building it says that you do not have access to "V:/Program files" I am using Windows Vista.




Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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





Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

Nope, it still errors as it building at (well its highlighted in yellow)

Code Snippet

objStreamOutput = New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FileMode.CreateNew)


saying that 'Access to the path 'V:\Users\Ben Ford\AppData\Local' is denied.'




Re: Visual Basic Language Include .mdb file in the .exe

Aw Ali

It appears that you have no access to the specified directory. Make sure that you have a access to the directory, and if not, talk to the sys administrator or use another less restrictive directory.




Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

Ok, thanks, how do i make it that it extracts it to the current directory




Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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

Code Snippet
objStreamOutput = New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FileMode.CreateNew)

with

Code Snippet

objStreamOutput = New FileStream(System.IO.Directory.GetCurrentDirectory() & "\" & "filename.mdb", FileMode.CreateNew)

Cheers

Nic





Re: Visual Basic Language Include .mdb file in the .exe

kidwizz

Ok, one more problem... the database it creates is only 0 bytes in side and VB says that it isn't valid.




Re: Visual Basic Language Include .mdb file in the .exe

Nicolas_qc_au

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

Code Snippet

System.IO.File.Delete(strFileName)

Where strFileName is the same parameter as used to output to the mdb file.

Cheers

Nic