Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL connectionstring
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 20-Nov-12 0:53am
pykos560
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Use the below technique to do the same.
 
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.
  Permalink  
v2
Comments
pykos at 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 at 20-Nov-12 6:58am
   
This should work When you change the mode to release.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100