简体   繁体   中英

Modifying connection string of an ASP.NET GridView -> BLL -> DAL at runtime

I am constructing a page to display a GridView of client data with pagination. My aspx page has a GridView whos DataSourceID is set to an ObjectDataSource. The ObjectDataSource is bound to aa BLL which in turn accesses the data through a DAL. I have the whole thing up and running while pointing to a static database. However, each client's data is stored in its own database. The next step is to modify the ConnectionString of the DAL depending on client login.

I have configured the DAL TableAdapter with the option ConnectionModifier set to 'Public'. My BLL can modify the connection string of the DAL, however I do not know how to pass to the BLL the client database name.

public class PDFDocumentsBLL {
  private PDFTableAdapter _pdfdocumentsadapter = null;
  protected PDFTableAdapter Adapter {
    get {
      if ( _pdfdocumentsadapter == null ) {
        _pdfdocumentsadapter = new PDFTableAdapter();
        _pdfdocumentsadapter.Connection = new System.Data.SqlClient.SqlConnection(
          ConfigurationManager.ConnectionStrings["template"].ConnectionString.Replace( "TEMPLATE", "TESTCLIENT" )
          );
      }
      return _pdfdocumentsadapter;
    }
  }
  ...
}

I would like to replace the string "TESTCLIENT" in the above code with a variable, but I am at a loss on how to pass this information to the BLL.

You may create some kind of database name provider that will return database name based on username, like

public class DataBaseNameProvider
{
   public string GetDataBaseName()
   {
      var userName = Membership.GetUser().UserName;
      return GetDatabaseNameByUserName(userName);
   }
}

And call that class from your BLL.

If you don't like idea to use ASP.NET stuff in your BLL because you don't want to add additional dependency, you still can create a wrapper around your BLL that will be Membership aware and will create your BLL passing username there.

If you are using Windows Authentication, then you can simply use

ConfigurationManager.ConnectionStrings[WindowsIdentity.GetCurrent().Name]

And might be good practice to retrieve the whole connection string for each user, it makes it more flexible, so you could use a completely different type of database if needed.

What I ended up doing is adding a PDFDB property to my BLL:

public class PDFDocumentsBLL {
  private PDFTableAdapter _pdfdocumentsadapter = null;
  public string PDFDB = "PDF_TEMPLATE";
  protected PDFTableAdapter Adapter {
    get {
      if ( _pdfdocumentsadapter == null ) {
        _pdfdocumentsadapter = new PDFTableAdapter();

        _pdfdocumentsadapter.Connection = new System.Data.SqlClient.SqlConnection(
          ConfigurationManager.ConnectionStrings["pdf"].ConnectionString.Replace( "PDF_TEMPLATE", PDFDB )
          );
      }
      return _pdfdocumentsadapter;
    }
  }
}

I then modified the GetBy/FillBy functions to take the DB as an additional parameter, and configured the ObjectDataSource to pass that value in from the Session variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM