I am creating a C# windows form applications, the working can be summarized as users fills some form and data is saved in Access database. Now the problem I am facing is that I have to deliver this as a setup file to someone. What I am thinking is that the code once installed on other computers and executed will give errors because of the connection string of Access db, as it will not match with that computer. I know that if a distribute projects I can put connection string in app.config and every user can change it according to his/her machine. But as I am giving a setup file how to solve this problem.
-
This is a though one... I've tried this before but could not get it done try this custom dialogue creator maybe a a starting point. Good luck and please post the answer once you get it.Mr.GT– Mr.GT2012-06-14 07:31:45 +00:00Commented Jun 14, 2012 at 7:31
-
Are you able to show app config file with setup in start>All Programs ?Sunny– Sunny2012-06-14 07:37:18 +00:00Commented Jun 14, 2012 at 7:37
4 Answers
Suppose you deploy your app.config with this connectionstring
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\yourFile.accdb;"
In a WinForms application the |DataDirectory| shortcut represent your application working folder, but you can change at runtime where it points to using this code.
// appdomain setup information
AppDomain currentDomain = AppDomain.CurrentDomain;
//Create or update a value pair for the appdomain
currentDomain.SetData("DataDirectory", "Your user choosen path");
It eliminates the need to hard-code the full path which, has you have discovered, leads to several problems to resolve during install. Of course your setup should deliver your database in your user choosen path.
6 Comments
You can build ConnectionString at run-time by 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 = "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();
Edit: you can use this : Finding SQL Servers on the Network
I have met this problem before. I solve it in such a way.
(1)in your app.config file, put a placeholder in the connection string. the connection string will contains the file path of the access db file. replace the path with a special string.
<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)when your application start, use Directory.GetCurrentDirectory to get app path.
before create a connection, replace the ##path## with the actual path on client computer.
static void test()
{
string s = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
s.Replace("##path##", Directory.GetCurrentDirectory());
OleDbConnection conn = new OleDbConnection(s);
}