A Big Strike Against TableAdapters

I'm hoping that someone reads this post and corrects me. But I'm not holding out much hope.

I don't normally use TableAdapters, but for a small application I decided that they seem like a reasonable choice. And so long as I was using them on my development platform, all was well and good. The problem arose when I delivered the application.

Like any good developer, I store the connection string to the data store in the configuration file. Which is what I did for this application. When I defined the TableAdapters, I pointed the connection string property to the same setting. Or so I thought. But what actually happened was that the connection string was actually stored in the DLL that contained the adapters. Hard-coded. As in not modifiable through the config files. And, naturally, this wasn't discovered until deployment.

So I started to look for ways to have the TableAdapter pull the connection string from the config files. I figured that this would be a fairly common scenario, so it should be address. Not so much

Apparently there is no way to automatically have TableAdapters use config settings. The "solution" is to use the fact that TableAdapters are partial classes to create a write-only ConnectionString property. It looks like the following:

public partial class FormTableAdapter
{
   public string ConnectionString
      {
         set
         {
            System.Data.SqlClient.SqlConnection conn = new 
               
System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = value;
            this.Connection = conn;
         }
      }
   }
}

Then, where the adapter is instantiated in your code, you set the ConnectionString property to the value from the config file.

To me, this is unacceptable. I was already a little skeptical of TableAdapters (I don't like the DataSet and the data access code in the same assembly), but this takes the cake. It almost seems like they were designed to not allow a reasonable deployment model. Maybe it will get better in the next version, but until then, I'm sticking with DataAdapters.

Comments

  • Erick Thompson May 2, 2007 8:09 PM

    Bruce,

    I'm not sure what happened here for you, but you should have had the option to store the connection string in the config when you ran the wizard. Can you email me?

    Erick Thompson

    ErickT awt Microsoft

  • Michael Neel July 30, 2007 2:10 PM

    I take it you are putting the TableAdapters in a DLL, then loading this in your project?  In this case, while using Wizard of the DataSet Designer do not save the connection string (since it will come from your application and not inside the DLL).  (The connection string stored with the XSD will be used by the designer)

    once you have your TableAdapter, Click the header (the bottom half of the box is the tableAdapter) and in the Properites look for ConnectionModifier and set it to Public or Internal, depending on the level you need.  Your tableAdapter will now have a property "Connection" and you can change the connection string in Connection.ConnectionString.  

    You can also use this to take control of opening/closing the connection and sharing a connection among many tableAdapters for performance. A tableAdapter get/Fill method chehs first if the connection is open, and if so uses that open connection and doesn't close it when done.

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS