By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

DatabaseConnection Strings

P: 6
Can anyone explain how these work in relation to setting and application setting files. I know that application settings can not be modified programatically at runtime, although they can be changed in the XML.
(I did try adding a set method in my settings.cs to allow me to update the application scoped databaseconnection string, but VS was not being foole d by that). However, I want to change my DatabaseConnection string (to point at a different database), but the property is an application and not a user one.

My objective is simple I'd like the user to be able to open a different sdf (database file) from the one currently in use, and for that to then be saved for next time.
Jan 10 '09 #1
Share this Question
Share on Google+
4 Replies


100+
P: 344
Hi,
Why didn't hard-code the connection string for a SqlDataSource and modify it using a OpenFileDialog.
Jan 10 '09 #2

P: 6
Hi,
Thank you! I did think of that but VS created the connection for me and I assume pointed the DataSet to it. How would\where would I modify my existing connection ? I can't find the SqlServer connection object that VS MUST (I guess) have created?
.
Jan 10 '09 #3

100+
P: 344
No instead of drag and drop the SqlDataSource,
create a class that accept either the name of the database or the absolute location of the database, i did some thing like this before.
Expand|Select|Wrap|Line Numbers
  1. public class DataClass
  2. {
  3.     private SqlDataSource sqlDataSource;
  4.     public DataClass(string name)
  5.     {
  6.         sqlDataSource = new SqlDataSource();
  7.         sqlDataSource.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\" + name + ";Integrated Security=True;User Instance=True";
  8.     }
  9.  
  10.     public SqlDataSource SqlDataSource
  11.     {
  12.         get { return sqlDataSource; }
  13.     }
  14. }
  15.  
now you can pass the name of the database and re-bind your control

Note: you have to store your queries as stored procedures in each database, or you have to store a very long dictionary, and that makes no sense.

OK, i did it for .mdf database and i'm not familiar with db, and you're using .sdf db. I don't know if there will be a problem ??!
Jan 10 '09 #4

Expert 100+
P: 190
Actually, you can modify your app.config file at runtime. You can also store multiple connection strings in the connectionString section, or you can dynamically add new ones as the application is running.

To see an example of opening, modifying and saving the app.config file, see: ConfigurationManager.RefreshSection Method (System.Configuration)

For an example of adding a new connectionString to the configuration file, see:
ConnectionStringSettingsCollection.Add Method (System.Configuration)
Jan 12 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.