错误1001 system.badimageformatexception无法在创建安装项目visual studio

本文关键字:安装 创建 项目 visual studio 1001 system badimageformatexception 错误 | 更新日期: 2023-09-27 18:27:31

我在Visual Studio 2013中有一个项目,使用wpf和Entity Framework连接到SQL Server Express R2数据库,该项目已经完成,我需要为它创建设置,我正在使用本教程
,所以我创建脚本(带数据)作为数据库的嵌入资源,安装程序类(名为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与数据库

我已经将数据库更改为非数据脚本,将主机添加到连接字符串,将目标从x64(当前)更改为任何CPU,但这些似乎都不起作用,请我需要一些指导。谢谢

错误1001 system.badimageformatexception无法在创建安装项目visual studio

听起来这与比特度不匹配有关。我在这里回答了一个类似的问题:

BadImageFormatException

具有安装程序类的程序集是64位的,但尝试运行它的installutil.exe必须是32位,这会导致问题。不知何故,您需要执行64位版本的installutil.exe.