0

How do I serialize DbConnectionStringBuilder object. I need to send the object from server to client.

Tried serializing the IDcitionary using this approach. But On client side, when I add those keys to object it assigns a funky connection string to it. And if you do

_connectionStringBuilder.ConnectionString = string.Empty;

That removes all the keys.

is it possible to return DbProviderFactory object from server to client ?

Cannon
  • 2,725
  • 10
  • 45
  • 86

1 Answers1

1

"StringDictionary not saving as user setting" shows a way of serializing DbConnectionString:

public static string Serialize(StringDictionary data)
{
    if(data == null) return null; // GIGO
    DbConnectionStringBuilder db = new DbConnectionStringBuilder();
    foreach (string key in data.Keys)
    {
        db[key] = data[key];
    }
    return db.ConnectionString;
}
public static StringDictionary Deserialize(string data)
{
    if (data == null) return null; // GIGO
    DbConnectionStringBuilder db = new DbConnectionStringBuilder();
    StringDictionary lookup = new StringDictionary();
    db.ConnectionString = data;
    foreach (string key in db.Keys)
    {
        lookup[key] = Convert.ToString(db[key]);
    }
    return lookup;
}

The StringDictionary would be used for serialization.

Community
  • 1
  • 1
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • I get your approach, but first time when I dont have connection string. I create this DbConnectionStringBuilder on server and return its keys. The moment I assign them to DbConnectionStringBuilder object on client it creates a funky connection string using those key-values. While I expect it to be empty before user actually adds a connection string on form. – Cannon Aug 24 '12 at 17:36
  • @buffer_overflow I'm not sure I follow--you're creating `DbConnectionStringBuilder` object and assigning keys to it and expecting it to be "empty" (i.e. no keys)? – Peter Ritchie Aug 24 '12 at 17:38
  • DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); Debug.Assert(factory != null); DbConnectionStringBuilder _connectionStringBuilder = factory.CreateConnectionStringBuilder(); Here _connectionStringBuilder has key-values but connectionstring is empty. Now I serialize this dict and send it back to client. On assigning this values to client _connectionStringBuilder object, it auto generates a connection string which is weird. – Cannon Aug 24 '12 at 17:43
  • Ahh, okay. When you create the `DbConnectionStringBuilder` that way, it instantiates a specific type of DbConnectionStringBuilder (like `SqlConnectionStringBuilder` which will have a read-only `Keys` collection specific to that provider. The values have not been set. The values are set/retrieved through the class indexer (like `_connectionStringBuilder["Application Name"] = "my application";` – Peter Ritchie Aug 24 '12 at 18:02
  • Or is it possible to return DbProviderFactory object from server to client ? – Cannon Aug 24 '12 at 18:16
  • The example code shows how to deserialize a StringDictionary into a DbConnectionStringBuilder. It's the values you want, the Keys are read-only and are only the names of the keys that you can assign values to. – Peter Ritchie Aug 24 '12 at 18:19