Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello!

I have a question about how to change between test and production databases in a windows forms app. So I built an app that inserts cerain values in a test database (reads someting from an xml and writes the resoults in the DB). I access the database thrugh the dataset(I draged the table from database connectino in VS onto the sufrace of the dataset). My question is, is there a way to switch betwen the production and test DB without having to create a new table, dataset or worse, an entire new app. I read something about just switching ConnectionStrings (initial catalog), although it does not throw an error nothing happenes in the database I specified.

Thanks in advance!
B
Posted

Hi,

Use the below technique to do the same.

C#
public static string GetConnectionString()
{
    #if (DEBUG)
      return testConnectionString;
    #else
      return productionConnectionString;
    #endif
}


To change the mode follow the below step.

1. Right click on root project.
2. Select properties
3. Select configuration
4. Change all the configuration to Release
5. Click Apply, Click Ok

Re-Run the application.

Hope this will help you.
 
Share this answer
 
v2
Comments
pykos 20-Nov-12 6:51am    
I have tried it this way but all the records get added in the test database, no matter if I set the conn string to point on the other database

#if (DEBUG)
adapter.Connection.ConnectionString = "Provider=SQLOLEDB;Data Source=******;Persist Security Info=True;Password=********;User ID=*******;Initial Catalog=DatabaseTest";
#else
adapter.Connection.ConnectionString = "Provider=SQLOLEDB;Data Source=******;Persist Security Info=True;Password=********;User ID=*******;Initial Catalog=DatabaseProduction";
#endif

I changed the mode to release.
Mohd. Mukhtar 20-Nov-12 6:58am    
This should work When you change the mode to release.
Save both Connection strings of the databases in the web.config App.config file using Cofiguration Manager.

based on your requirement use the if condition to assign Connection string to the SqlConnectio object.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900