Change the connection string after creating the installation file in C # .NET.

I am creating a Windows C # form application, the work can be summarized as users fill out some form and the data is stored in the Access database. Now the problem I am facing is that I have to provide this as an installation file to someone. I think that the code once installed on other computers and executed will give errors due to the Access db connection string, since it will not correspond to this computer. I know that if I distribute projects, I can set the connection string in app.config, and each user can change it according to his / her machine. But since I give the installation file, how to solve this problem.

+4
source share
3 answers

Suppose you deploy your app.config using this connection string

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

In a WinForms application, the shortcut |DataDirectory| represents the working folder of your application, but you can change it at run time, where it indicates the use of 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"); 

This eliminates the need for hard coding of the full path, which, as you find, leads to several problems during installation. Of course, your setup should deliver your database in the path you choose.

+3
source

You can build a 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: Search SQL servers on the network

0
source

I have already met this problem. I solve it this way.
(1) in the app.config file, put the placeholder in the connection string. the connection string will contain the path to the db access 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 starting the application, use Directory.GetCurrentDirectory to get the path to the application. Before creating the connection, replace ## path ## with the actual path on the client computer.

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

All Articles