簡體   English   中英

如何將連接字符串從 App.config 移動到 C# 中的代碼?

[英]How to move connectionString from App.config to code in C#?

我想從 App.config 中刪除 connectionString 並將其添加到代碼中。 我正在使用 WinForms(C#) 和 .Net Framework 4.8。 我的表單上有文本框和數據網格視圖。 我正在使用 Microsoft Access 數據庫 (.mdb)。 我想這樣做是因為數據庫的密碼在 App.config 中可見。 有沒有辦法做到這一點

這是我的 app.config XML 代碼:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Locker.Properties.Settings.pw_dbConnectionString"
            connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\files\pw_db.mdb;Persist Security Info=True;Jet OLEDB:Database Password=XYZ"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>

我建議您不要在代碼中定義連接字符串,最好在 app.config 文件中對其進行加密。

請按照以下步驟操作:

  • 將 app.config 重命名為 web.config
  • 打開命令提示符並鍵入:%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" web.config 的路徑>
  • 將 web.config 重命名回 app.config

如果您在記事本中打開文件,您將看到加密的值,您可以像以前一樣繼續使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM