TableAdapter Connection Strings
Posted on February 18, 2008
Filed Under .Net Code, CSharp, VB.Net
I don’t know how a lot of people handle it, but one thing that always bugged me about table adapters and datasets is the way it handles it’s connection strings. It starts out innocent enough. A new dataset is created with it’s associated table adapter. The connection string is saved to the application settings file and that property is saved in the dataset. Running it on the development machine (or on the same network) is no big deal and just works. But what if you send that application to someone else that has their own SQL Server?
Did anyone at Microsoft actually use this scenario in a production environment? What were they thinking?
In order to run that in a production environment you have to set the connection string in the app.config file. While this may be fine for some people, what about the people like me that do not want my users to access that database? I don’t want them to have that username and password… While the chances of a normal user loading SQL Server Management Studio and logging in are slim, it’s still possible and it’s definitely possible if a user purposely wants to get out of having to do work that day.
What are the options? Well, one option is to use the encryption to encrypt the settings in app.config. For me, this option is not ideal. Reports of those settings getting corrupted are quite high, plus you have to deal with the loading and saving of those settings, which isn’t all that easy to do.
The other option is to take the route I was taking for a while… I had a function that would build me a connection string. Then I could: TableAdapter.Connection.ConnectionString = myLibrary.ConnectionStringFunction()
This was great, until that day came where I was in a hurry and added a few more tables to a form but forgot to set the ConnectionStrings. Whoops.
So I needed a solution that would stop me from having to set those ConnectionString properties, keep my connection string out of the app.config, and be easy to use (i.e. Just Works).
I started out by just giving all my datasets the same connection string. Then on application startup, I tried to change that one application setting. Hmm.. It seems those ConnectionString properties are set to friend and are read only.
Upon further investigation, it seems that there are some events that fire, such as SettingsLoaded. This event fires when the app.config is read and all the settings are loaded. When this event fires, it fires inside the MySettings class. This should allow that property to be changed.
Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
Me.Item("MyAppConnectionString") = MyLibrary.BuildConnectionString()
End Sub
This will set the MyAppConnectionString setting to the proper connection string. Now, all table adapters will have an up to date connection string.
So what happens if you want to change the connection string later while the application is still running? Well, there is no way to do that. So it’s time to come up with a way to trick it into updating that property.
In looking at the MySettings class, there is another event called PropertyChanged. We can use this event if we create another setting that can be updated anywhere in the application. First, we create a new string setting that has a User scope (I called mine ConnectionString). This will allow the application to update the setting at any time.
Next, we need to create a function that will update that property with our connection string.
Public Shared Sub ChangeConnectionString()
My.Settings.ConnectionString = BuildConnectionString()
End Sub
Now we can change the events in the MySettings class to look like this.
Private Sub MySettings_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
If e.PropertyName = "ConnectionString" Then
Me.Item("MyAppConnectionString") = My.Settings.ConnectionString
End If
End Sub
Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
MyLibrary.ChangeConnectionString()
End Sub
End Class
Now, every time that ChangeConnectionString() is called, the MyAppConnectionString will be updated. The ChangeConnectionString procedure can be changed so that it can accept a string parameter that is the actual connection string. Then you can build a Connection String anywhere and just pass it to that procedure.
Comments
3 Responses to “TableAdapter Connection Strings”
Leave a Reply
easy way to change TableAdapter Connection runtime;
open the DataSetDesigner.cs where the DataSet contains the TableAdapter what you want to use.
Look at the class definition of the TableAdapter:
namespace DataSources.MyTableAdapters {
///
///Represents the connection and commands used to retrieve and save data.
///
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(”System.Data.Design.TypedDataSetGenerator”, “2.0.0.0″)]
[global::System.ComponentModel.DesignerCategoryAttribute(”code”)]
[global::System.ComponentModel.ToolboxItem(true)]
[global::System.ComponentModel.DataObjectAttribute(true)]
[global::System.ComponentModel.DesignerAttribute(”Microsoft.VSDesigner.DataSource.Design.TableAdapterDesigner, Microsoft.VSDesigner” +
“, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)]
[global::System.ComponentModel.Design.HelpKeywordAttribute(”vs.data.TableAdapter”)]
public partial class myTableAdapter : global::System.ComponentModel.Component {
private global::System.Data.OleDb.OleDbDataAdapter _adapter;
.
.
.
.
Look at the Connection property definition, and change the following line to the next one;
internal global::System.Data.OleDb.OleDbConnection Connection {
public global::System.Data.OleDb.OleDbConnection Connection {
build the dataset project, and you can access to the Connection property of the TableAdapter in the future.
While that works well, it will get reset back if you make any changes in the Visual Studio designer and you would have to do it again. Aside from changing the connection string, with table adapters, there is no other reason to access the connection property (at least for me, anyway). With the solution above, I don’t even need to see the connection property anymore. It just changes when my user changes the server or database to connect to.
When you have to work with more databases (same structure) from an application thats a “good enough reason” to me.
And another good reason; You cannot pass an opened connection to the TableAdapter, but that always opens the prepared one, than close it again. Yes I know there are the code in the DataSet definition, what checks the state of the connection, and won’t open again if thats opened, but I don’t know other way to give an opened connection to the TableAdapter. I think, in some cases, and for some type of applications this is not a real problem, but when your application do lot of requests to the database server, this can slow that down.