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 tha
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);
}
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.
In case of deploying the application, along with SQLLocalDB, the user (machine\user) should be added in your database to have access.