0

同時にいくつかのデータベースを切り替える必要があります (VB.net を使用)。私の知る限り、いくつかの接続文字列を作成し、それらをさまざまな条件で使用する必要があります。

例えば

If ( condition )  then 
  use connection String 1
elseif (condition)  then 
  use connection String 2 
end if 

誰でもこれを処理する方法を手伝ってもらえますか? MSSQL 2005 と VB.net を使用しています

4

2 に答える 2

2

条件外で変数を宣言しますIf-Else。このようなことを試してみてください。

Dim ConnectionStr As String = String.empty
If (condition) Then
    ConnectionStr = 'conString 1'
ElseIf (condition) Then
    ConnectionStr = 'conString 2'
Else
    ConnectionStr = 'others....'
End if

' ConnectionStr hold your latest connection string
于 2012-10-18T04:08:18.013 に答える
1

In VB.NET, you need to add a module and write few functions there. And use these function to establish and close connection to Database.

Variables: ConnectionString variables (both)

Functions:

  • Close_Database_Connection(): If connection is open then close it.
  • Create_Database_Connection(condition_param): Here you put your
    conditions in If Else Structure. Like:

Code:

If ( condition ) Then
        use connection String 1 
    ElseIf (condition) Then
        use connection String 2 
    End If

Use this "Create_Database_Connection(condition_param)" Method where you need to establish connection in code depending upon you conditional parameters.

于 2012-10-18T04:37:38.803 に答える