在运行时设置强类型数据集连接字符串的最佳方法?

问题描述 投票:0回答:8

我的 Windows 窗体应用程序使用使用 Visual Studio 中的设计器创建的强类型数据集。在运行时,我希望能够选择实时数据库或测试数据库。

在运行时以编程方式设置数据集连接字符串的最佳方法是什么?

c# visual-studio ado.net
8个回答
3
投票

TableAdapters 中的连接属性定义为 internal

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

因此,如果您的 TypedDataset 不在 与主窗口相同的程序集 表单应用程序,您将无法 访问连接属性。这 当您稍后可能会出现问题 重构您的数据集代码并移动它 到一个单独的项目中,该项目将 生产自己的独立组件。

要解决这个问题,您可以按照以下方法进行。

为 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);

2
投票

默认情况下,

Connection
属性设置为
internal
。这可以在数据集的设计器中更改。

  1. 右键单击 TableAdapter。

  1. 然后将
    ConnectionModifier
    属性更改为
    public

  1. 您现在可以访问项目中的
    Connection
    属性。
var loginsTableAdapter = new MyDataSetTableAdapters.LoginsTableAdapter();
loginsTableAdapter.Connection.ConnectionString = _myConnectionString;

1
投票

将它们的连接字符串存储在 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 或与任何其他开关一起使用将为您提供测试配置。


1
投票

回复: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;
    }
}

请注意,如果重新生成数据集,您可能会丢失这部分代码,并且如果不重构数据集,您可能必须将其添加到多个对象中。


1
投票

编辑设计器文件很痛苦。

我在“User”下创建了一个名为“ConnectionString”的设置条目,这使得 Visual Studio 在添加强类型数据集时创建一个应用程序字符串“Connection String1”。

因此,我只需将数据集设计器文件中的所有“ConnectionString1”替换为“ConnectionString”,这将允许您使用“用户”字符串设置在运行时加载连接字符串。

恕我直言,这是一个缺点,允许用户在运行时修改连接字符串。 (有人在雷德蒙德听吗?)


0
投票

使用 TableAdapterManager 可能适合您。请阅读更多信息:http://rajmsdn.wordpress.com/2009/12/09/strongly-typed-dataset-connection-string/


0
投票

迄今为止我找到的最佳解决方案:

添加另一个程序设置,用于保存客户端在运行时设置的首选连接字符串(例如 newConnectionString)

然后在使用 Table Adapter 之前:

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

0
投票

这么多年过去了,我遇到了这个问题,是别人 2009 年的遗留代码和一卡车的表适配器造成的。

我在分部类中扩展了 Settings 并更改了 OnLoad 事件中的连接字符串。

namespace LegacyProgram.Properties
{
    using System.Configuration;

    internal sealed partial class Settings
    {

        protected override void OnSettingsLoaded(object sender, SettingsLoadedEventArgs e)
        {
            base.OnSettingsLoaded(sender, e);
            SetConnectionString(App.Settings.ConnectionString);
        }

        internal void SetConnectionString(string value)
        {
            this["ConnectionString"] = value;
        }     
    }
}

在我的例子中,我添加了一个静态的“App.Settings”,它从 JSON 而不是旧的 exe.config 加载,并且连接字符串实际上是使用 SqlConnectionStringBuilder 构建的。

© www.soinside.com 2019 - 2024. All rights reserved.