强类型数据集的动态连接字符串

发布于 2024-08-19 07:03:19 字数 245 浏览 1 评论 0原文

我有一个 asp.net nTier 应用程序。数据访问层是一个强类型的DataSet,由多个带有DataAdapter 的DataTable 组成。当用户登录时,他们选择要连接到的数据库(从成员资格数据库中的表)。我需要将选定的连接字符串传递到 DataSet 对象中,该对象对于该用户来说将保持不变,直到他们再次登录。

我认为答案可能是创建 DataSet 对象的部分类,我可以在其中将连接字符串传递到构造函数中。但我不知道该怎么做。

干杯

I have an asp.net nTier application. The data access layer is a strongly typed DataSet consisting of multiple DataTables with DataAdapters. When the user logs in, they choose which database to connect to (from a table in the membership database). I need to pass the selected connection string into the DataSet object which will remain the same for that users until they log in again.

I'm thinking that the answer might be to create a partial class of the DataSet object where I can pass the connection string into the constructor. Im not sure how to go about this though.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友谊不毕业 2024-08-26 07:03:19

您可以通过部分课程来做到这一点。

假设您的类型化数据集名为 HurrDurr:

public partial class HurrDurr
{
  public HurrDurr(string connex)
  {    
    this._connection = new global::System.Data.SqlClient.SqlConnection();
    this._connection.ConnectionString = connex;
  }
}

仅当第一次访问 Connection 内部属性时 _connection 为 null 时才会初始化。

You could do this with a partial class.

Assuming your typed dataset is called HurrDurr:

public partial class HurrDurr
{
  public HurrDurr(string connex)
  {    
    this._connection = new global::System.Data.SqlClient.SqlConnection();
    this._connection.ConnectionString = connex;
  }
}

_connection is only initialized if it is null the first time the Connection internal property is accessed.

池予 2024-08-26 07:03:19

终于弄清楚了这件事的真相。在一个新模块中,我为表适配器创建了一个分部类,这是我需要更改连接字符串的地方,我最初犯的一个错误是没有指定正确的名称空间。

下面是我创建的部分类,它允许我动态更改名为 tblOptions 的表的表适配器之一的连接字符串:

Namespace ds1TableAdapters
    Partial Public Class tblOptionsTableAdapter
        Sub ChangeConnString(ByVal newConn As String)
            Me._connection.ConnectionString = newConn
        End Sub
    End Class
End Namespace

感谢 Will 的帮助,它让我朝着正确的方向前进。

Finally got to the bottom of this. In a new module I created a partial class to the table adapter which is where I needed to change the connection string, one mistake I was making originally was not specifying the correct namespace.

Below is the partial class I created which allowed me to dynamically change the connection string of one of my table adapters for a table called tblOptions:

Namespace ds1TableAdapters
    Partial Public Class tblOptionsTableAdapter
        Sub ChangeConnString(ByVal newConn As String)
            Me._connection.ConnectionString = newConn
        End Sub
    End Class
End Namespace

Thanks for the help Will, it got me going in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文