【发布时间】:2014-10-21 23:09:58
【问题描述】:
我在 Visual Studio 2013 中有一个项目,它使用 wpf 和实体框架并连接到 SQL Server Express R2 数据库,该项目已完成,我需要为其创建设置,我正在使用 this tutorial
所以我创建脚本(带有数据)作为数据库的嵌入式资源,安装程序类(名为 SetupInstaller)并将项目的输出添加到安装程序,一切都没有错误地构建,但是当我尝试安装项目时我收到错误
错误 1001 System.BadImageFormatException 无法加载文件或程序集
我做了一些更改,这只是在我创建自定义操作时发生,但是当我不使用它时,不会创建数据库
我的连接字符串去
<connectionStrings>
<add name="Bulldog_Gym.Properties.Settings.BulldogGymConnectionString"
connectionString="Data Source=.;Initial Catalog=BulldogGym;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
我的 SetupInstaller 类是这样的
[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");
}
}
还有我的txt和数据库
我已经将数据库更改为非数据脚本,将 master 添加到连接字符串,将目标从 x64(当前)更改为 Any CPU,但这些似乎都不起作用,我需要一些指导。 谢谢!
【问题讨论】: