C# 项目 - InstallShield,安装 运行 后出现 MySql 错误

C# Project - InstallShield , MySql error when run after installation

我在 Visual Studio 2013 年创建了一个 C# 项目。我想使用 Install Shield 免费版创建一个安装程序。我创建了一个安装程序并尝试在其他计算机上 运行 它,但是当我 运行 它并尝试登录程序时,我遇到了 mysql 数据库的问题。错误信息是:

Unhandled exception has occured in you application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

Cannot connect.

如果我点击详细信息按钮,我会出现一系列错误,这些错误涉及 Mysql。示例:

System.Exception: Cannot connect ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts.
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at simulator.ConnConfig.getConnection()

simulator 是项目的名称。 ConnConfig 是一个 class 连接,getConnection()ConnConfig 的函数,其中 return 连接。我试过在另一台电脑上安装.NET Framework 4.5.2, SQL Server, 也没用。 在我的项目中,我使用 localhost 服务器,其中我有一个包含 2 个表的数据库。我的问题是,是否有可能将该本地主机数据库添加到安装程序并在另一台计算机上使用它?哪些可再发行组件需要此操作?此外,我已经在计算机上安装了 .NET Framework 4.5,Sql Server 2012..但是当我尝试通过 Redistributables 在 InstallShield 中添加它们时,它一直说 Needs to be downloaded。为什么?

更新

我有这个 Class 我在其中进行连接。但我在该行收到错误:Additional information: Illegal characters in path.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace simulator
{
    class ConnConfig
    {
        private static string conn = "Data Source=c:\aplicatie.db;Version=3;Password=myPassword;";
        public static SQLiteConnection connect = null;

        private ConnConfig()
        {

        }
        public static SQLiteConnection getConnection()
        {
            SQLiteConnection connect = null;
            try
            {
                connect = new SQLiteConnection(conn);
                connect.Open();// here I receive the error
                return connect;
            }
            catch (SQLiteException e)
            {
                throw new Exception("Cannot connect", e);
            }
        }
    }
}

更新 3

在 class 中,我连接到数据库。在其他形式中,我只使用表格及其数据。为了连接到程序,我使用了一个登录表单,该表单使用 class 到 getConnection()。我通过在模拟器项目中添加 ADO.NET 创建了该数据库。随之而来的是我在本地主机服务器中已有的那两个表。所以,如果我必须用新表创建另一个空数据库没关系,但是在哪里包含该代码或如何使用它,因为我不明白该脚本是如何工作的..我应该把它放在哪里?

如果你的数据库要安装在每个客户端上,而且你的表不是很大,你可能想看看像 SQLite 这样的轻量级的东西,它不需要任何安装,只需要 dll,而且非常快速和高效,而且只当你的程序运行时运行。

关于需要下载的问题,看来你没有正确设置你的先决条件,你可以按照本文中的步骤进行更正 Adding InstallShield Prerequisites, Merge Modules, and Objects to Basic MSI and InstallScript MSI Projects

您可能需要考虑确定 MySQL 是否适合您。

SQLite vs MySQL vs PostgreSQL: A Comparison Of Relational Database Management Systems

并查看 SQLite 的局限性SQLite

因为老实说,在每个系统上安装 MySQL 似乎有点矫枉过正。如果您在网络上有一台带有 MySQL 的服务器,可以。但在每个系统上似乎都是个坏主意。

至于连接到 SQLite 数据库,这里是 List of Connection Strings for SQLite

请参阅本主题,了解如何创建数据库和表 Create SQLite Database and table

您可以通过 ODBC 访问 SQLite。

我的猜测是您的程序在部署时没有将数据库与设置捆绑在一起。这可能是因为您没有在 项目文件 中将数据库标记为 数据文件 。试试这个,在 Solution Explorer 和 select Properties 菜单中右键单击您的项目名称。在水平选项卡中单击 Publish 选项卡。在 安装模式和设置 下单击 应用程序文件 按钮。出现一个包含所有应用程序文件的对话框。从相应单元格的下拉列表中将数据库 发布状态 设置为 数据文件。这样您的数据库将在发布时与设置捆绑在一起。希望这有帮助。