iT邦幫忙

0

[C#] 多個轉檔程式如何共用同個app.config 或 connectString ?

各位前輩下午好

小弟使用C#寫了多個轉檔程式(winForm),每個專案都有包了一個app.config及相關的dll檔,現在想要全部丟到一個資料夾底下去執行,好處只需要放一個dll檔,若app.config也能只有一個,以後若是要修改資料庫連線位置,或者sa密碼,也非常簡單快速。

我google後,有的文章說將app.config以加入連結的方式,加進專案裡面,但我編譯完成後,在Debug資料還是會出現一個跟專案名稱一樣的.exe.config,請問是哪裡做錯嗎? 可以教我一下嗎?? 謝謝!

附上我的app.config

<?xml version="1.0"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="OracleDB" connectionString="Data Source=;User ID=;Password=;" providerName="Oracle.DataAccess.Client"/>
    <add name="SQLDB" connectionString="Server=;Database=;User ID=sa;Password=;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.serviceModel>
    <bindings/>
    <client/>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
  </startup>
</configuration>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
yj0803
iT邦新手 5 級 ‧ 2019-07-25 22:57:56

可以先在config裡面新增一個連接字串的路徑變數
然後再用configurationmanager.openmappedexeconfiguration
去指定上面那一個連接字串的config檔

b7307024 iT邦新手 3 級 ‧ 2019-08-02 10:59:45 檢舉

您好,我參考您給的關鍵字去查詢,找到一篇文章
https://stackoverflow.com/questions/1682054/how-do-i-retrieve-appsettings-from-the-assembly-config-file

但是我不知道該怎麼使用,我將程式碼放在public partial class Form1 : Form 卻會有錯誤訊息
放在private void Form1_Load 卻不會,於是我就卡關了。

b7307024 iT邦新手 3 級 ‧ 2019-08-07 11:45:59 檢舉

有前輩可以教學嗎?將感激不盡!

我要發表回答

立即登入回答