2010-07-03 8 views
3

因此,這裏是最新的。我剛剛在Visual C#中創建並「發佈」了一個人員管理工具。在開發過程中,我使用了一個保存在Properties.Settings.Default中的字符串來連接到我用於開發的數據庫。現在,由於解決方案已發佈並準備就緒,老闆想要連接到真正的員工數據庫。我的印象是,連接到新的數據庫就像在某些屬性文件中更改連接字符串一樣簡單。不幸的是,我似乎無法找到正確的文件/字符串連接到我想要的數據庫。有任何想法嗎?如何將發佈的Visual C#解決方案連接到不同的數據庫

謝謝! JB

回答

2

看吧:

Connection Strings and Configuration Files

使用您剛剛有一次你的應用程序已經部署到更改配置文件的連接字符串配置文件。

這裏做的方式,你想要什麼:

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

您的配置文件的內容:

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

方法來獲取連接字符串在運行時:

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; 
} 

使用方法:

string connectionString = GetConnectionString("YourName"); 
+0

很酷。我會盡快嘗試並與您取得聯繫。 – JnBrymn 2010-07-04 14:23:29

+0

好的,我試過了,失敗了...... *但是*我很接近。我有通過''引用外部文件中連接字符串的app.config文件。當我調試代碼時,我可以將connections.config文件放在Debug \ bin目錄中,一切正常。但是當我去發佈它時,解決方案不會將connections.config放在適當的目錄中......並且我不知道該目錄是什麼。發佈解決方案時,我應該在哪裏放置connections.config? – JnBrymn 2010-07-05 17:41:04

+0

請看這裏:http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx您應該將文件connections.config複製到輸出目錄。 – 2010-07-07 04:05:59

0

我必須更改Properties.Settings文本文件中的條目,重新編譯並重新部署以獲取新的連接字符串。將來可以考慮在ConnectionStrings或AppSettings節點下的.config文件中讀取連接字符串。當你存儲它時,你只需要改變一個生產文本文件來切換你的數據庫...

相關問題