Quantcast
Channel: DataSource Controls - SqlDataSource, ObjectDataSource, etc.
Viewing all articles
Browse latest Browse all 956

ASP.NET modify connectionstring at runtime

$
0
0

I need to change dataset connectionstrings to point to different DBs at run time.

I've looked at a number of solutions however they all seem to be related to WinForms or web application projects or other technology slightly different than what I'm using, so I haven't figured out how apply them.

The application is like a discussion. It's a web site project based on code originally written under VS2005, and there's no budget (or personal talent!) for major changes at this time. The app is written in vb.net; I can understand answers in c#. I'm working in VS2013.

For example, the following post has lots of solutions, however, they seem to focus on web application projects, that have a project file with settings, which this web site project does not. 

https://social.msdn.microsoft.com/Forums/en-US/7483b816-be7a-4204-a4d3-cfb14b2aae26/how-to-dynamically-change-connection-string-in-generated-dataset-class?forum=adodotnetdataset

The app has three typed datasets pointing to one MDF, call it "MainDB.mdf". There are dozens of tableadapters among the three datasets.

I'm going to deploy the app it as an "alpha/demo" version. I would like to use the same code base for all users, and a separate physical version of MainDB for each user, to reduce chances that the users crash each other.

The initial demo access URL will contain query string information that I can use to connect the user with the right physical database file. I should be able to identify the database name and thus the connection string parameters from the query string information (probably using replace on a generic connection string). If necessary I could use appsettings to store fully formed connection strings, however, I would like to avoid that.

I would like to be able to change the connection strings for all the datasets at the time that the entry point pages for the app are accessed.

Changing the tableadapter connection strings at each instantiation of the tableapters would (maybe) require too much code change (at least a couple of hundred instantiations); I'dmaybe just make complete separate sites instead of doing that. That's the fall back position if I can't dynamically change the connectionstrings at runtime (or learn some other way to make this general scheme work).

Below is a sample instantiation of tableadapter (admittedly not great code, however, it works for the demo version).

Any suggestions on how to approach this would be appreciated.

Thanks!

Public Shared Sub ClearOperCntrlIfHasThisStaff( _
    varSesnID As Integer, varWrkprID As Integer)

    Dim TA As GSD_DataSetTableAdapters.OPER_CNTRLTableAdapter

    Dim DR As GSD_DataSet.OPER_CNTRLRow
    DR = DB.GetOperCntrlRowBySesnID(varSesnID)

    If IsNothing(DR) Then
        Exit Sub
    End If

    If DR.AField = varWrkprID Then
        DR.AField = -1
        TA.Update(DR)
        DR.AcceptChanges()
    End If

End Sub

Viewing all articles
Browse latest Browse all 956

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>