创建安装文件后,我创建一个C#Windows窗体应用程序,工作可以概括为用户填充某种形式和数据保存在Access数据库。现在我面临的问题是我必须将此作为安装文件提供给某人。我在想的是,曾经安装在其他计算机上并执行的代码会因为Access数据库的连接字符串而出错,因为它与该计算机不匹配。我知道,如果分发项目,我可以将连接字符串放在app.config中,并且每个用户都可以根据他/她的机器进行更改。但正如我给安装文件如何解决这个问题。更改连接字符串在C#.NET
回答
您可以通过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
这是一个他们连接到的Access数据库,而不是SQL Server。 – cjk
MS Access DB是ODBC/OLEDB。然后上面的工作,只需更改连接字符串。 –
我以前见过这个问题。我以这种方式解决它。
(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);
}
假设你用这个的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");
它不再需要硬编码的完整路径它,拥有你已经发现,导致几个问题要解决在安装过程中。当然,你的设置应该在用户选择的路径中传送数据库。
但是什么时候我们有SQL服务器?它包含根据计算机名称的实例名称。 – Freelancer
如果你发布的MDF文件你自己,那么你应该与AttachDbFileName财产尝试为[这里解释(http://msdn.microsoft.com/en-us/library/ms247257(V = VS.80)的.aspx)但是如果您已经安装了数据库,那么您可以尝试使用自定义安装程序来请求数据源主机。另一方面,您可以向您的程序添加一个配置选项,该选项会在向您的用户安装此信息之后询问并重新配置您的连接字符串 – Steve
我仅通过attach db附加数据库。但如果在我的机器实例是。/ SQLExpress和其他的,根据计算机名称,例如steve/SQLExpress或其他,它有时可能会有所不同。在这种情况下,它完全依赖于实例名称[连接字符串中的服务器名称],我们应该怎么做? – Freelancer
- 1. 更改连接字符串
- 2. 更改连接字符串
- 3. 更改连接字符串
- 4. 连接字符串在C#.NET
- 5. 更改连接字符串值在app.config
- 6. Informix .NET连接字符串
- 7. .Net PostgreSQL连接字符串
- 8. 在C#中使用字符串连接的字符串连接
- 9. 动态更改连接字符串
- 10. 动态更改ProfileProvider连接字符串
- 11. 更改nopCommerce的连接字符串?
- 12. C#在运行时更改自定义连接字符串
- 13. 当不能连接到第一个连接字符串时,C#更改为不同的连接字符串
- 14. 连接字符串为c#.net到mysql数据库连接
- 15. C#.NET连接字符串无法连接
- 16. 字符串连接和线程在.NET
- 17. C#/ LINQ:连接字符串
- 18. 连接字符串C#
- 19. C#Oracle连接字符串
- 20. MySQL连接字符串C#
- 21. 从C#代码更改连接字符串?
- 22. 在web.config更新后,Edmx连接字符串不会更改
- 23. 更改字符串值。 C#
- 24. Informix for .NET的连接字符串
- 25. ADO中的连接字符串。 Net
- 26. Sybase ODBC连接字符串与.net
- 27. 连接字符串问题Oracle .Net
- 28. .NET字符串连接(+&+ =)与StringBuilder
- 29. 在C++中连接字符串
- 30. 在宏中连接字符串 - C++
这是一个虽然一个...我以前试过,但不能把它做尝试[**这个定制对话创造者**](http://www.codeproject.com/Articles/18834/Create - 可视化Studi中使用的自定义对话框)可能是一个起点。祝你好运,一旦你得到它,请张贴答案。 –
您是否能够在开始>所有程序中显示设置配置文件? – Sunny