0

For the project I am working on I have created an empty database as a template file and need it to be included in my project.

The file is only being used when the program copies it to a new location. After that all the work is with the new file. My code is doing exactly what I want; I just do not want to add a new datasource simply to copy one file.

Sample code :

Imports System.IO    
Imports System.Data.OleDb    
Imports System.Windows.Forms    

FName = Directory & NewFile & ".mdb"    
FName2 = Directory & NewFile & ".rdb"    
'File.Copy(Application.StartupPath & "\Template.mdb", FName, False) 'old 
IO.File.WriteAllBytes(FName, My.Resources.Template)                 'new
Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FName)    
Try    
  Con.Open()    
  SQLstr = String.Format("INSERT INTO tblParts (FILENAME) VALUES('{0}');", Name)    
  Command = New OleDbCommand(SQLstr, Con)    
  Command.ExecuteNonQuery()
Finally    
  Con.Close()    
End Try

'File.Copy(FName, FName2, False) 'old
'File.Delete(FName)              'old
FileSystem.Rename(FName, FName2) 'new

Can I add a database file into a Visual Studio project without adding a new datasource?

Solution: Added the file as a resource in Project Properties and cancelled the Datasource Wizard. I also cleaned up the code for creating and renaming the file. Old code is commented for reference.

1 Answer 1

0

yes, you can add almost any kind of file to a project, using Rclick -> Add Existing Item. then make sure the file is set to "Content" in its properties, and that its set to "Copy if newer". that way whenever you change your template, on next build, the file will be copied to the \bin\debug or \bin\release directory depending on your build configuration.

if you are doing an ASP project, just drop it in your bin directory, since websites don't build the same way as other project types. beware however that a website in deployment should never run under a user that is allowed to modify any of the files within the site, including copying them, so this is not a good approach for web.

2
  • This worked. The 'Data Source Configuration Wizard' opened when I used Rclick -> Add Existing Item so I thought I had to use it. I clicked Cancel on the wizard and it automatically added the file with the 'Build Action' set to 'Content'.
    – Wayne
    Nov 26, 2013 at 17:33
  • wonderful. feel free to mark the answer accepted if everything works out for you. Nov 26, 2013 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .