9

Windows アプリケーション内で asp.net のメンバーシップ プロバイダーを使用するデータベースの接続文字列をプログラムで変更したいと考えています。system.configuration 名前空間ではユーザー設定を変更できますが、アプリケーション設定を調整したいですか? クラスを変更するために XML を使用してクラスを作成する必要がありますか? 現在の接続を削除して (クリアする接続を選択できます)、新しい接続を追加する必要がありますか? 既存の接続文字列を調整できますか?

4

4 に答える 4

9

この正確なことをしなければなりませんでした。これは私のために働いたコードです:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["Blah"].ConnectionString = "Data Source=blah;Initial Catalog=blah;UID=blah;password=blah";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
于 2012-01-09T22:49:19.320 に答える
8
// Get the application configuration file.
System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.None);

// Create a connection string element and
// save it to the configuration file.

// Create a connection string element.
ConnectionStringSettings csSettings =
        new ConnectionStringSettings("My Connection",
        "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
        "Initial Catalog=aspnetdb", "System.Data.SqlClient");

// Get the connection strings section.
ConnectionStringsSection csSection =
    config.ConnectionStrings;

// Add the new element.
csSection.ConnectionStrings.Add(csSettings);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
于 2008-09-15T14:59:49.307 に答える
6

System.configuration名前空間を使用して、プログラムで構成を開くことができます。

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

次に、次の場所で接続文字列コレクションにアクセスできます。

myConfig.ConnectionStrings.ConnectionStrings

コレクションは必要に応じて変更でき、完了したら.Save()構成オブジェクトを呼び出します。

于 2008-09-15T14:50:11.513 に答える
0

ConnectionStringsSectionクラスを使用します。ドキュメントには、新しいConnectionStringを作成し、XMLシバン全体を実装せずにフレームワークに構成ファイルに保存させる方法の例も記載されています。

ここを参照して、例を参照してください。

于 2008-09-15T14:56:13.760 に答える