2012-06-14 82 views
4

创建安装文件后,我创建一个C#Windows窗体应用程序,工作可以概括为用户填充某种形式和数据保存在Access数据库。现在我面临的问题是我必须将此作为安装文件提供给某人。我在想的是,曾经安装在其他计算机上并执行的代码会因为Access数据库的连接字符串而出错,因为它与该计算机不匹配。我知道,如果分发项目,我可以将连接字符串放在app.config中,并且每个用户都可以根据他/她的机器进行更改。但正如我给安装文件如何解决这个问题。更改连接字符串在C#.NET

+0

这是一个虽然一个...我以前试过,但不能把它做尝试[**这个定制对话创造者**](http://www.codeproject.com/Articles/18834/Create - 可视化Studi中使用的自定义对话框)可能是一个起点。祝你好运,一旦你得到它,请张贴答案。 –

+0

您是否能够在开始>所有程序中显示设置配置文件? – Sunny

回答

0

您可以通过SqlConnectionStringBuilder

// 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 = "[email protected]"; 
    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(); 

编辑在运行时建立ConnectionString你可以使用这个:Finding SQL Servers on the Network

+0

这是一个他们连接到的Access数据库,而不是SQL Server。 – cjk

+0

MS Access DB是ODBC/OLEDB。然后上面的工作,只需更改连接字符串。 –

0

我以前见过这个问题。我以这种方式解决它。
(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); 
} 
+0

我可以用很少的代码解释这一点。提前致谢! – Imran

+0

我已经粘贴了代码,它运行良好。但我认为史蒂夫的答案更加灵活。你可以像史蒂夫说的那样尝试。 – Jerry

3

假设你用这个的ConnectionString

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

在WinForms应用程序的快捷方式|DataDirectory|代表您的应用程序工作文件夹部署的app.config,但你可以在运行时它指向使用这种改变码。

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

它不再需要硬编码的完整路径它,拥有你已经发现,导致几个问题要解决在安装过程中。当然,你的设置应该在用户选择的路径中传送数据库。

+0

但是什么时候我们有SQL服务器?它包含根据计算机名称的实例名称。 – Freelancer

+0

如果你发布的MDF文件你自己,那么你应该与AttachDbFileName财产尝试为[这里解释(http://msdn.microsoft.com/en-us/library/ms247257(V = VS.80)的.aspx)但是如果您已经安装了数据库,那么您可以尝试使用自定义安装程序来请求数据源主机。另一方面,您可以向您的程序添加一个配置选项,该选项会在向您的用户安装此信息之后询问并重新配置您的连接字符串 – Steve

+0

我仅通过attach db附加数据库。但如果在我的机器实例是。/ SQLExpress和其他的,根据计算机名称,例如steve/SQLExpress或其他,它有时可能会有所不同。在这种情况下,它完全依赖于实例名称[连接字符串中的服务器名称],我们应该怎么做? – Freelancer