2013-03-25 109 views
5

我的C#應用​​程序中使用DataSetTableAdapters。它們是從VS2008 GUI工具生成的。什麼是處理在Windows窗體應用程序連接字符串的最佳方法是什麼?

例子:

右鍵項目 - >添加新項 - >數據集

此方法會自動添加一個連接字符串到app.config

但是,這是連接字符串硬繩方法。我想以簡單的方式更改連接字符串。但是,當我使用數據集,然後連接字符串從應用程序屬性中獲取。 這種情況是否有解決方案?

enter image description here

這是我的連接字符串店Settings.Designer.cs文件

namespace WindowsFormsApplication2.Properties { 


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] 
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 

     private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 

     public static Settings Default { 
      get { 
       return defaultInstance; 
      } 
     } 

     [global::System.Configuration.ApplicationScopedSettingAttribute()] 
     [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
     [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)] 
     [global::System.Configuration.DefaultSettingValueAttribute("Data Source=SLCERT\\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True")] 
     public string TestDataBaseConnectionString { 
      get { 
       return ((string)(this["TestDataBaseConnectionString"])); // this is the connection string get from the dataset's 
      } 
     } 
    } 
} 

的app.config包含

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="WindowsFormsApplication2.Properties.Settings.TestDataBaseConnectionString" 
      connectionString="Data Source=SLCERT\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True" 
      providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 
+0

什麼應用程序做的不是簡單的方法? – alerya 2013-03-25 08:22:28

+2

什麼比編輯app.config更簡單? – 2013-03-25 08:24:30

+1

應用程序。配置伴隨您的應用程序。它的XML。可以在任何文本編輯器中輕鬆編輯 – 2013-03-25 08:25:04

回答

6

我相信你問這個,所以您不必在本地測試和生產/測試服務器之間手動更改。

你可能想看看How to: Transform Web.config When Deploying a Web Application Project

它是關於web.config中,而不是app.config中卻是同樣的想法。

ps。僅2010年VS以上

+0

是的!我問,但現在的Web應用程序,它是Windows窗體應用程序 – Elshan 2013-03-25 08:25:37

+0

:)然後好猜。更多信息在這裏:http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/或這裏http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx – 2013-03-25 08:32:42

+0

問題更新 – Elshan 2013-03-25 09:04:44

4

除了由JP Hellemons提供做配置轉換的建議,還有別的東西可以做,因爲這是隻由.NET 4.0支持(本地)。您可以添加任意數量的連接字符串配置文件的<connectionStrings/>部分的,所以添加一個「DebugConnectionString」和「ReleaseConnectionString」,或類似的。現在

,爲了使用這些不加干預,每次環境發生了變化,你可以使用跟蹤常量。說當地在Visual Studio中你與DEBUG不變集編譯和發佈部署當它不存在,那麼你可以做類似如下:在app.config中

#if DEBUG 
    return ConfigurationManager.ConnectionStrings["DebugConnectionString"]; 
#else 
    return ConfigurationManager.ConnectionStrings["ReleaseConnectionString"]; 
#endif 
+0

這種方法可以,但是當我使用DataSet,我想在每次發佈之前重新配置它(DataSet是由VS2008 GUI工具生成的,然後它會自動將連接字符串添加到settings.cs文件中) – Elshan 2013-03-25 08:48:52

+0

問題更新 – Elshan 2013-03-25 09:04:53

2

<configuration> 
<configSections> 
</configSections> 
<connectionStrings> 
    <add name="DBCS" connectionString="Data Source=|DataDirectory|\Database.sdf;password=Password" 
     providerName="Microsoft.SqlServerCe.Client.3.5" /> 
</connectionStrings> 

和訪問它像這樣

static string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString(); 

包括

System.Configuration; 

命名空間

+0

這對於一般建議是可以的。但是當我使用DataSet(使用GUI工具的數據集,VS自動通過設置文件添加連接字符串。如果要在app.config中使用連接字符串,我想要替換該部分) – Elshan 2013-03-25 08:45:58

+1

。創建一個類從數據庫中獲取數據,將其存儲在類中,並調用該類來填充控件,如datagridview。 – XTGX 2013-03-25 09:28:50

相關問題