1

Currently I am using SQL Server CE for persisting my data for which I am providing with a .sdf and connection string mentioned in app.config pointing to this .sdf file.

Now I want to provide user with the flexibility to have the data stored in their own SQL Server database if present at there disposal.

Now I am facing the problem of how to change the connection string at runtime if user chooses to uses its own database ?

Or if restrict them to use my predefined .mdf file how to attach that in their SQL Server ?

2
  • That'll be tough - since for SQL Server CE, you need to use stuff like SqlCeConnection and SqlCeCommand while for a "real" SQL Server, those are SqlConnection and SqlCommand. Just switching the connection string isn't enough .... Commented Feb 9, 2012 at 7:45
  • Unless you use DbConnection DbCommand etc.... Commented Feb 9, 2012 at 7:47

2 Answers 2

1

My recommendation would be to have 2 connection strings in the configuration file (app or web). There is a special section for them intuitively called ConnectionStrings. You can then switch between them based on other settings.

Sign up to request clarification or add additional context in comments.

Comments

0

Changing connection strings dynamically is actually pretty easy to do as long as you have a place to store the new settings (ie. web or app config files). If you provide a way for them to enter the server information you can use the ConfigurationManager class to update your app/web.config.

Ado.net typically has parameters on almost any db connection object that allows you to specify the connection string as an arguement. Additionally, there are helper classes that can be used to construct the connection string on the fly like the SqlConnectionStringBuilder or EntityConnectionStringBuilder. I personally love the Entity Framework as it allows you to create the database from the model itself if it does not already exist, provided you already have the connection string.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.