Serialize DbConnectionStringBuilder

时间:2012-08-24 16:11:20

标签: c# odbc oledb

如何序列化DbConnectionStringBuilder对象。我需要将对象从服务器发送到客户端。

尝试使用this方法序列化IDcitionary。但是在客户端,当我将这些键添加到对象时,它会为它分配一个时髦的连接字符串。如果你这样做

_connectionStringBuilder.ConnectionString = string.Empty;

删除所有密钥。

是否可以将DbProviderFactory对象从服务器返回到客户端?

1 个答案:

答案 0 :(得分:1)

StringDictionary not saving as user setting”显示了序列化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;
}

StringDictionary将用于序列化。