簡體   English   中英

在運行時設置強類型數據集連接字符串的最佳方法?

[英]Best way to set strongly typed dataset connection string at runtime?

我的 Windows 窗體應用程序使用使用 Visual Studio 中的設計器創建的強類型數據集。 在運行時,我希望能夠選擇實時數據庫或測試數據庫。

在運行時以編程方式為數據集設置連接字符串的最佳方法是什么?

TableAdapters 中的連接屬性定義為internal

internal global::System.Data.SqlClient.SqlConnection Connection

因此,如果您的 TypedDataset 與您的主 Windows 窗體應用程序不在同一個程序集中,您將無法訪問 Connection 屬性。 當您重構數據集代碼並將其移動到將生成其自己獨立程序集的單獨項目中時,此問題可能會在稍后彈出。

要解決此問題,您可以按如下所述進行操作。

為您的 TableAdapter 創建部分類並在默認公共無參數構造函數旁邊添加另一個構造函數。 假設 TableAdapter 類型為 MyTableAdapter

public partial class MyTableAdapter
{
    public MyTableAdapter(SqlConnection connection)
    {
        thisSetConnection(connection);
        this.ClearBeforeFill = true;
    }

    public void SetConnection(SqlConnection connection)
    {
        this._connection = connection;
    }
}

您需要為項目中的 TableAdapter 執行此操作。 TableAdapter 沒有任何公共基類,但感謝它們被聲明為部分類,因此我們能夠按照上述方式進行操作。

現在在運行時,您可以像這樣創建 TableAdapter 的實例。

SqlConnection connection;
//create the connection here at runtime..
MyTableAdapter adapter = new MyTableAdapter(connection);

或者甚至可以在您使用默認的無參數公共構造函數創建 TableAdapter 實例之后再分配它..

SqlConnection connection;
//create the connection here at runtime..
MyTableAdapter adapter = new MyTableAdapter();
adapter.SetConnection(connection);

默認情況下, Connection屬性設置為internal 這可以在數據集的設計器中更改。

  1. 右鍵單擊 TableAdapter。

在此處輸入圖片說明

  1. 然后將ConnectionModifier屬性更改為public

在此處輸入圖片說明

  1. 您現在可以訪問項目中的Connection屬性。
var loginsTableAdapter = new MyDataSetTableAdapters.LoginsTableAdapter();
loginsTableAdapter.Connection.ConnectionString = _myConnectionString;

編輯設計器文件很痛苦。

我在“用戶”下創建了一個名為“ConnectionString”的設置條目,這使 Visual Studio 在您添加強類型數據集時創建應用程序字符串“Connection String1”。

因此,我只是將數據集設計器文件中的所有“ConnectionString1”替換為“ConnectionString”,這將允許您使用“User”字符串設置在運行時加載您的連接字符串。

恕我直言,這是一個缺點,允許用戶在運行時修改連接字符串。 (有人在雷德蒙德聽嗎?)

將它們的連接字符串存儲在 app.config 中,然后您可以根據命令行/啟動開關進行切換。 或者,如果您想為用戶提供靈活性,您可以為他們提供一個選項頁面,他們可以在其中選擇要使用的連接。

下面是讀取啟動開關的代碼:

string[] args = Environment.GetCommandLineArgs();
// The first (0 index) commandline argument is the exe path.
if (args.Length > 1)
{
    if (Array.IndexOf(args, "/live") != -1)
    {
        // connection string = 
        // ConfigurationSettings.AppSettings["LiveConString"];
    }
}
else
{
    // connection string = 
    // ConfigurationSettings.AppSettings["TestConString"];
}

所以現在你通過調用以下命令來啟動你的應用程序:

MyApp.exe /live

單獨使用 MyApp.exe 或與任何其他開關一起使用將為您提供測試配置。

回復:wethercotes 評論

當您設置數據集時,向導會存儲連接字符串,但這並不意味着您不能使其動態化。 如何取決於您使用的版本,但通常如果您展開數據集下的文件,您會找到類似 Designer.cs 或 DataTableNameAdapter.xsd 的文件。 您可以打開這些文件並搜索 _connection。 這通常是一個私有變量,並在類的 init 函數中設置。

您可以通過添加如下代碼使設置動態化:

public string ConnectionString
{
    get { return this._connection.ConnectionString; }
    set
    {
        if (this._connection == null)
        {
            this._connection = new System.Data.SqlClient.SqlConnection();
        }
        this._connection.ConnectionString = value;
    }
}

請注意,如果您重新生成數據集,您可能會丟失這部分代碼,並且如果不重構數據集,您可能必須將其添加到多個對象中。

使用 TableAdapterManager 可能對您有用。 請閱讀更多信息: http : //rajmsdn.wordpress.com/2009/12/09/strongly-typed-dataset-connection-string/

迄今為止我找到的最佳解決方案:

添加另一個程序設置,其中包含客戶端在運行時設置的首選連接字符串(例如 newConnectionString)

然后在使用表適配器之前:

this.myTableAdapter.Connection.ConnectionString = Properties.Settings.Default.newConnectionString;

暫無
暫無

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

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