Cari Blog Ini

04 Oktober 2013

How To Create Connection with Different Database Server

Apply To :
* Microsoft SQL Server 2000
* Microsoft Visual Basic 6

Want to :
* Create connection with different Database Server





Step - step :

1. Example Table :







2. Try below code,

'-------------------------------------------------------------------------------------------------
Private Sub Form_Load()

Dim conn_1, conn_2 As ADODB.Connection
Dim rs_1, rs_2 As ADODB.Recordset

'------------------------------------------------------------------
'------------------------------------------------------------------
'------------------------------------------------------------------
'-----Connection to Server 1, Example
'------------------------------------------------------------------
'------------------------------------------------------------------
        
        Set conn_1 = New ADODB.Connection
        conn_1.ConnectionString = "Provider=SQLOLEDB;Data Source=SERVER1;Initial Catalog=DB_SIMULASI;user Id=sa;password=sasasa"
        conn_1.Open
        Set rs_1 = New ADODB.Recordset
        rs_1.CursorLocation = adUseServer
        rs_1.Open "select * from AdjusterType", conn_1, 1, 1
        
        
        MsgBox "Record Count SERVER 1 - Table AdjusterType is " & rs_1.RecordCount

        rs_1.Close
        conn_1.Close


'------------------------------------------------------------------
'------------------------------------------------------------------
'------------------------------------------------------------------
'-----Connection to Server 2, Example
'------------------------------------------------------------------
'------------------------------------------------------------------

        Set conn_2 = New ADODB.Connection
        conn_2.ConnectionString = "Provider=SQLOLEDB;Data Source=SERVER2;Initial Catalog=DB_TEST;user Id=sa;password=sasasa"
        conn_2.Open
        Set rs_2 = New ADODB.Recordset
        rs_2.CursorLocation = adUseServer
        rs_2.Open "select * from Branch", conn_2, 1, 1
        
        MsgBox "Record Count SERVER 2 - Table Branch is " & rs_2.RecordCount
        
        rs_2.Close
        conn_2.Close


End Sub

'-------------------------------------------------------------------------------------------------











3. The results is