开发者

Dynamic Connection String for a Strongly Typed Dataset

开发者 https://www.devze.com 2022-12-17 04:44 出处:网络
I have an asp.net nTier application. The data access layer is a strongly typed DataSet consisting of multiple DataTables with DataAdapte开发者_如何学编程rs. When the user logs in, they choose which da

I have an asp.net nTier application. The data access layer is a strongly typed DataSet consisting of multiple DataTables with DataAdapte开发者_如何学编程rs. 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消