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

13

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

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


1
从VS2010开始,可以指定连接的可见性(例如public),并且可以指定生成的DataTableAdapters的基类...(也就是说,这篇文章主要涉及到VS2008和之前的问题 :-)) - user166390
7个回答

3

TableAdapters中的连接属性被定义为internal

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

如果您的 TypedDataset 不在主窗体应用程序相同的程序集中,您将无法访问 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);

2
默认情况下,Connection属性被设置为internal。这可以在DataSet的设计器中更改。
  1. 右键单击TableAdapter。

enter image description here

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

enter image description here

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

1

编辑设计文件真是一件痛苦的事情。

我在“用户”下创建了一个名为“ConnectionString”的设置项,这样当您添加强类型数据集时,Visual Studio会创建一个应用程序字符串“ConnectionString1”。

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

在我看来,允许用户在运行时修改连接字符串是一个缺陷。(有人在Redmond听吗?)


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单独或与任何其他开关一起使用将为您获取测试配置。


谢谢您的回复,但我的问题是如何在运行时设置强类型数据集的连接字符串。 - wethercotes

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;
    }
}

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

再次感谢Gary。我已经创建了一个包含您的代码的部分类,以防数据集重新生成时丢失它。不幸的是,这必须针对每个数据适配器进行操作,而我的数据适配器有数十个。 - wethercotes

0

0
到目前为止,我找到的最佳解决方案是:
添加另一个程序设置,该设置保存客户端在运行时设置的首选连接字符串(例如newConnectionString)。
然后在使用表适配器之前:
this.myTableAdapter.Connection.ConnectionString = Properties.Settings.Default.newConnectionString;

不,你不能这样做。TableAdapters中的Connection属性被定义为internal。 - this. __curious_geek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接