TableAdapterの接続文字列変更

TableAdapterで使用している接続文字列の変更は

  • DataSet「foo」の上で、barTableAdapterを作成。
  • 1の作成時に、接続文字列を「csDB」という名前でapp.configへ保存。
  • barTableAdapterをフォーム上に配置。
  • フォームのコード中で、接続文字列を
Me.barTableAdapter.Connection.ConnectionString = "接続文字列"

と再設定。

これで変更できた。
けれどソース内で

Dim ta As New fooTableAdapters.barTableAdapter

とすると、taにはConnectionというメソッド自体が見当たらないっぽい。
仕方ないのでSetting.Designer.vb

Public ReadOnly Property csDB() As String
    Get
        Return CType(Me("csDB"), String)
    End Get
End Property

となっている部分を

Public Property csDB() As String
    Get
        Return CType(Me("csDB"), String)
    End Get
    Set(ByVal value As String)
        Me("csDB") = value
    End Set
End Property

と、値の変更可能な形に変更。
あとはフォームのLoadイベント内辺りで

My.Settings.csDB = "接続文字列"

としてやったら変更できた。
ただし、これが使い方としてアリかどうかは不明。でもapp.configにはパスワード書きたくないしなあ。
ちなみにC#で同じことをする場合は、Setting.Designer.csの

public string csDB {
    get {
        return ((string)(this["csDB"]));
    }
}

public string csDB {
    get {
        return ((string)(this["csDB"]));
    }
    set
    {
        this["csDB"] = value;
    }
}

に変更。

ソース本文では、

Properties.Settings.Default.csDB= "接続文字列";

という感じで変更すればおk。
try文の中で変更して、finallyで元の文字列に戻すと安全。