I have a project in Visual Studio 2013 using wpf and Entity Framework with a conection to a SQL Server Express R2 database, the project is finished and I need to create the setup for it, I'm using this tutorial
so I create the scripts (with data) as embedded resource for the database, the Installer Class (named SetupInstaller) and add the ouput of the project to the installer, everything builds with no error, but when I try to install the project I receive the error
Error 1001 System.BadImageFormatException Could not load file or assembly
I did several changes and this just happens when I create the custom action, but when I don't use it the database is not created
My conection string goes
<connectionStrings>
<add name="Bulldog_Gym.Properties.Settings.BulldogGymConnectionString"
connectionString="Data Source=.;Initial Catalog=BulldogGym;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
My SetupInstaller class goes like this
[RunInstaller(true)]
public partial class SetupInstaller : System.Configuration.Install.Installer
{
SqlConnection masterConnection = new SqlConnection();
public SetupInstaller()
: base()
{
InitializeComponent();
}
private string GetSql(string Name)
{
try
{
// Gets the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();
// Resources are named using a fully qualified name.
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
// Reads the contents of the embedded file.
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("In GetSQL: " + ex.Message);
throw ex;
}
}
private void ExecuteSql(string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);
// Initialize the connection, open it, and set it to the "master" database
masterConnection.ConnectionString = Properties.Settings.Default.BulldogGymConnectionString;
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
// Closing the connection should be done in a Finally block
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
// Creates the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName);
// Creates the tables.
ExecuteSql(strDBName, GetSql("bulldog.txt"));
// Creates the stored procedure.
//ExecuteSql(strDBName, GetSql("getproduct.txt"));
}
catch (Exception ex)
{
// Reports any errors and abort.
MessageBox.Show("In exception handler: " + ex.Message);
throw ex;
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable("BulldogGym");
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
ExecuteSql("master", "DROP DATABASE BulldogGym");
}
}
And my txt with the database
I already change the database to non data script, add the master to the connection string, change the target from x64 (current) to Any CPU but none of this seems to work, please I need some guidance. Thanks!