【问题标题】:Change Connection String After Creating Setup File in C#.NET在 C#.NET 中创建安装文件后更改连接字符串
【发布时间】:2018-04-26 01:02:37
【问题描述】:

我正在创建一个 C# windows 窗体应用程序,其工作可以概括为用户填写一些表单并将数据保存在 Access 数据库中。现在我面临的问题是我必须将其作为安装文件交付给某人。我在想的是,由于Access db的连接字符串,一旦在其他计算机上安装并执行的代码会出错,因为它与那台计算机不匹配。我知道,如果分发项目,我可以将连接字符串放在 app.config 中,每个用户都可以根据他/她的机器更改它。但是当我给出一个安装文件时,如何解决这个问题。

【问题讨论】:

  • 这是一个虽然...我之前尝试过但无法完成尝试this custom dialogue creator也许是一个起点。祝你好运,一旦你得到答案,请发布。
  • 你能在开始>所有程序中显示应用配置文件吗?

标签: c# winforms


【解决方案1】:

假设您使用此连接字符串部署 app.config

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\yourFile.accdb;"

在 WinForms 应用程序中,|DataDirectory| 快捷方式表示您的应用程序工作文件夹,但您可以在运行时更改它指向使用此代码的位置。

// appdomain setup information
AppDomain currentDomain = AppDomain.CurrentDomain;
//Create or update a value pair for the appdomain
currentDomain.SetData("DataDirectory", "Your user choosen path");

它消除了对完整路径进行硬编码的需要,您已经发现,在安装过程中会导致一些问题需要解决。当然,您的设置应该在您的用户选择的路径中提供您的数据库。

【讨论】:

  • 但是当我们有 SQL 服务器时呢?它包含根据计算机名称的实例名称。
  • 如果您自己分发 MDF 文件,那么您应该尝试将 AttachDbFileName 属性设置为 explained here,但如果您已经安装了数据库,那么您可以尝试使用要求数据源的自定义设置过程主持人。另一方面,您可以在程序中添加一个配置选项,在安装后向您的用户询问此信息并重新配置您的连接字符串
  • 我只通过附加数据库来附加数据库。但是,如果在我的机器实例上是 ./SQLExpress 而在其他机器上,它有时会根据计算机名称(例如 steve/SQLExpress 或其他任何名称)而有所不同。它完全取决于实例名称[连接字符串中的服务器名称] 在这种情况下我们应该怎么做?
  • 如果您的应用程序是独立的,没有数据共享(我的意思是数据库仅供本地计算机上的应用程序使用),那么您可以尝试deploy a LocalDBLocalDB 是 Sql Express 2012 版本,它没有t 需要完整的服务实例和更简单的安装模式blogs.msdn.com/b/sqlexpress/archive/2011/07/12/…
  • 下面的代码应该放在哪里: // appdomain 设置信息?我真的不知道这一点,因为我没有尝试使用数据库部署应用程序。请赐教。
【解决方案2】:

您可以通过SqlConnectionStringBuilder 在运行时构建ConnectionString

// Create a new SqlConnectionStringBuilder and
    // initialize it with a few name/value pairs.
    SqlConnectionStringBuilder builder =
        new SqlConnectionStringBuilder(GetConnectionString());

    // The input connection string used the 
    // Server key, but the new connection string uses
    // the well-known Data Source key instead.
    Console.WriteLine(builder.ConnectionString);

    // Pass the SqlConnectionStringBuilder an existing 
    // connection string, and you can retrieve and
    // modify any of the elements.
    builder.ConnectionString = "server=(local);user id=ab;" +
        "password= a!Pass113;initial catalog=AdventureWorks";

    // Now that the connection string has been parsed,
    // you can work with individual items.
    Console.WriteLine(builder.Password);
    builder.Password = "new@1Password";
    builder.AsynchronousProcessing = true;

    // You can refer to connection keys using strings, 
    // as well. When you use this technique (the default
    // Item property in Visual Basic, or the indexer in C#),
    // you can specify any synonym for the connection string key
    // name.
    builder["Server"] = ".";
    builder["Connect Timeout"] = 1000;
    builder["Trusted_Connection"] = true;
    Console.WriteLine(builder.ConnectionString);

    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();

编辑:你可以使用这个:Finding SQL Servers on the Network

【讨论】:

  • 他们连接的是 Access 数据库,而不是 SQL Server。
  • MS Access DB 是 ODBC/OLEDB。那么上面的就可以了,只要改连接字符串就好了。
【解决方案3】:

我以前遇到过这个问题。我是这样解决的。
(1) 在您的 app.config 文件中,在连接字符串中放置一个占位符。连接字符串将包含访问数据库文件的文件路径。用特殊字符串替换路径。

  <connectionStrings>
    <!-- original connection string , change it to the below line -->
    <!--  <add name="test" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source=d:\test\test.mdb "/> -->
    <add name="test" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;   Data   Source=##path##\test.mdb "/>
  </connectionStrings>

(2)当您的应用程序启动时,使用 Directory.GetCurrentDirectory 获取应用程序路径。 在创建连接之前,请将 ##path## 替换为客户端计算机上的实际路径。

static void test()
{
    string s = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
    s.Replace("##path##", Directory.GetCurrentDirectory());
    OleDbConnection conn = new OleDbConnection(s);
}

【讨论】:

  • 我已经粘贴了代码,效果很好。但我认为史蒂夫的回答更灵活。你可以按照史蒂夫说的试试看。
  • 嘿,@Jerry 我怎样才能在那里访问“conn”?例如conn.close();
【解决方案4】:

在部署应用程序以及 SQLLocalDB 的情况下,应将用户 (machine\user) 添加到您的数据库中以进行访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    相关资源
    最近更新 更多