开发者

How to connect published Visual C# solution to a different database

So here's the what's up. I just created and "published" a staff management tool in Visual C#. During development, I used a string saved in Properties.Settings.D开发者_如何转开发efault to connect to the database I was using for development. Now since the solution is published and ready to go, the boss wants to connect to the real staff database. I was under the impression that connection to the new database would be as simple as changing the connection string in some properties file somewhere. Unfortunately I can't seem to find the proper file/string to connect to the database I want to. Any ideas?

Thanks! JB


Look here:

Connection Strings and Configuration Files

By using a config file you just have to change the config file connection string once your application has been deployed.

Here's a way of doing what you want:

From http://www.dreamincode.net/forums/topic/70745-connection-string-in-appconfig/

Your config file content:

<connectionStrings >
<add name="YourName"
connectionString="Provider=msdaora;Data Source=MyOracleDB;Persist Security Info=False;Integrated Security=Yes;"
providerName="System.Data.OracleClient" />
</connectionStrings> 

Method to get the connection string at runtime:

public static string GetConnectionString(string strConnection)
{
 //Declare a string to hold the connection string
 string sReturn = new string("");
 //Check to see if they provided a connection string name
 if (!string.IsNullOrEmpty(strConnection))
 {
  //Retrieve the connection string fromt he app.config
  sReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString;
 }
 else
 {
  //Since they didnt provide the name of the connection string
  //just grab the default on from app.config
  sReturn = ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString;
 }
 //Return the connection string to the calling method
 return sReturn;
}

Using the method:

string connectionString = GetConnectionString("YourName");


I've had to change the entry in the Properties.Settings text file, recompile and redeploy to get the new connectionstring to take. In the future consider reading your connection string from your .config file under either the ConnectionStrings or AppSettings node. When you store it there you simply need to change a production text file to switch your database...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜