4

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.

2
  • 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
    Jun 14, 2012 at 7:31
  • Are you able to show app config file with setup in start>All Programs ?
    – Sunny
    Jun 14, 2012 at 7:37

4 Answers 4

3

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
  • but what when we have SQL server? It contains instance name according to the name of computer.
    – Freelancer
    Jun 6, 2013 at 11:05
  • If you distribute the MDF file yourself then you should try with the AttachDbFileName property as explained here, but if you have already the database installed then you could try with a custom setup procedure that asks for the datasource host. On the other hand you could add a configuration option to your program that asks after the install this information to your user and reconfigure your connection string
    – Steve
    Jun 6, 2013 at 11:15
  • I attaches DB by attach db only. But if on my machine instance is ./SQLExpress and on others it can sometimes be different according to computer name such as steve/SQLExpress or any else. It totally depends upon instance name[server name in connection string] in that case what should we do?
    – Freelancer
    Jun 6, 2013 at 11:21
  • If your app is standalone without data sharing (I mean the database is used only by your application on a local machine) then you could try to deploy a LocalDB LocalDB is a Sql Express 2012 version that doesn't require a full service instance and with a easyer installation pattern blogs.msdn.com/b/sqlexpress/archive/2011/07/12/…
    – Steve
    Jun 6, 2013 at 11:28
  • Where do I put the code below: // appdomain setup information? I really have no idea about this since I have not tried deploying an app with a database. Please enlighten me.
    – paraJdox1
    Dec 27, 2020 at 6:09
0

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

2
  • It's an Access database they are connecting to, not SQL Server.
    – cjk
    Jun 14, 2012 at 7:41
  • MS Access DBs are either ODBC/OLEDB. Then the above will work, just change the connection string.
    – Mr.GT
    Jun 14, 2012 at 7:44
0

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);
}
2
  • I have pasted the code, it works well. But I think Steve's answer is more flexible. You can try it as Steve said.
    – Jerry
    Jun 14, 2012 at 8:12
  • Hey, @Jerry how will I be able to access "conn" there? e.g. conn.close();
    – paraJdox1
    Dec 27, 2020 at 6:23
0

In case of deploying the application, along with SQLLocalDB, the user (machine\user) should be added in your database to have access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.