Page 1 of 2

connection two SQL server and two databases

PostPosted: Thu May 21, 2015 9:26 am
by mtajkov
I use SQLRDD and ODBC connection for MYSql. How I open One table from one sql server and at the same time Open second table from second server?

Regards,
Milos

FWHX 12.08

Re: connection two SQL server and two databases

PostPosted: Thu May 21, 2015 10:33 am
by Antonio Linares
Milos,

Have you considered to use ADORDD ? It is free and you have full source code for it

Re: connection two SQL server and two databases

PostPosted: Thu May 21, 2015 10:50 am
by mtajkov
The application has been in development for several years, and I'm not sure how the changes in the code were necessary?

Regards,
Miloš

Re: connection two SQL server and two databases

PostPosted: Thu May 21, 2015 12:36 pm
by Rick Lipkin
Miloš

This is not a problem .. just create two connections oCn1to one database and oCn2 to the other database and make sure when you open your recordset you pass the correct connection to manipulate your tables.

If it were me, I would be a bit more descriptive on my connection name like:

oCnServer1
oCnServer2

Rick Lipkin

Re: connection two SQL server and two databases

PostPosted: Thu May 21, 2015 4:00 pm
by mtajkov
this is first connection:
Code: Select all  Expand view
cDatabase=AllTrim(System->Database)
 
      If ! cDatabase IN hIniFile
         MsgInfo("Connection [" + cDatabase + "] not found in sqlrdd.ini")
         Quit
      EndIf

      hDsn := hIniFile[ cDatabase ]

      If !"CONNSTRING" IN hDsn
         MsgInfo( "ConnString not found in " + cDatabase)
         Quit
      EndIf

   aDsn    := OdbcDsnEntries()
 
   if len(aDsn) == 0
      MsgAlert("Mora te kreirati ODBC DSN konfiguraciju iz vašeg kontrol panela",;
               "ODBC DSN selekcija")
      return nil
   endif

 cConnString := hDsn[ "CONNSTRING" ]

if !Connect(cConnString)
  return
endif

oSql   := SR_GetConnection()
 


and this second connection:
Code: Select all  Expand view
USE System NEW

hIniFile := HB_ReadIni( "sperdd.ini", .F.,,.F. )     // Read ini file in a hash table

 sDatabase=AllTrim(System->Database)


      If ! sDatabase IN hIniFile
         MsgInfo("Connection [" + sDatabase + "] not found in sperdd.ini")
         Quit
      EndIf

      hDsn := hIniFile[ sDatabase ]

      If !"CONNSTRING" IN hDsn
         MsgInfo( "ConnString not found in " + sDatabase)
         Quit
      EndIf

   aDsn    := OdbcDsnEntries()
 
   if len(aDsn) == 0
      MsgAlert("Mora te kreirati ODBC DSN konfiguraciju iz vašeg kontrol panela",;
               "ODBC DSN selekcija")
      return nil
   endif

 cConnString := hDsn[ "CONNSTRING" ]

if !Connect(cConnString)
  return
endif

oSqlSpe   := SR_GetConnection()
 


if I try from first connection (server/databases..):
USE Inf_Firma SHARED VIA "SQLRDD" NEW
It is okay.

But if I try to open the same table with another server (base) is not functioning. The application is stopped without notice error.The same is true if you change the order.

Regards,
Miloš

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 9:18 am
by avista
May be this help

I use 7 servers via VPN
I use one connection and SQL statements something like this

SELECT * FROM DataBase1@Server1:table
SELECT * FROM DataBase2@Server2:table
.
.
.

Best regards,

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 12:46 pm
by mtajkov
I try
Code: Select all  Expand view
                    nErr := oSql:exec("INSERT INTO &cKnjiga (GK_Godina, GK_Firma, GK_Nalog, GK_Datum, GK_Dokum, GK_Opis, GK_Konto, GK_Analit, GK_DugPoc, GK_PotPoc, GK_Duguje, GK_Potraz)"+;
                               " SELECT GK_Godina, '" + mFirma_id + "', GK_Nalog, GK_Datum, GK_Dokum, GK_Opis, GK_Konto, GK_Analit, GK_DugPoc, GK_PotPoc, GK_Duguje, GK_Potraz FROM Infobit@Server:Spe_Knjiga"+;
                               " WHERE &cFirma .Gk_Godina= '" + mObrGod + "'")
 
but get a syntax error:

Detaljan opis greške
--------------------
Error SR_ODBC/0 SQLExecDirect Error
42000 - 102 - [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near 'Infobit@Server:'.
Command sent to database :
INSERT INTO TMP_MISA_KNJIGA (GK_Godina, GK_Firma, GK_Nalog, GK_Datum, GK_Dokum, GK_Opis, GK_Konto, GK_Analit, GK_DugPoc, GK_PotPoc, GK_Duguje, GK_Potraz) SELECT GK_Godina, 'SPE', GK_Nalog, GK_Datum, GK_Dokum, GK_Opis, GK_Konto, GK_Analit, GK_DugPoc, GK_PotPoc, GK_Duguje, GK_Potraz FROM Infobit@Server:Spe_Knjiga WHERE SPE_KNJIGA .Gk_Godina= '2015'
Steatment handle :
Connection handle :
RetCode : -1
- RollBack executed.

Regards

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 1:30 pm
by Antonio Linares
Miloš,

SQLRDD is a commercial product so surely asking its developers may help you

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 1:35 pm
by Antonio Linares
I have just sent the error to Mr. Rao as he is our best SQL ADO expert :-)

Lets see what he says

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 3:01 pm
by Antonio Linares
His answer:

First thing I want to tell you is that in T-SQL ( Transact SQL of MS Server ), all variables are prefixed with "@".
@Server means it is a variable by name "@Server".

We should not leave any variable name as it is in an Sql statement All should be constants.

If "infobit" is the server name, then Infobit must be substituted in the place of variable "@Server"

Instead of
Infobit@Server:Spe_Knjiga
It should be Infobit.Spe_knjiga
( full syntax is <Server>.<schema>.<tablename>)

For linked servers to work
we should link the server first by executing
sp_addlinkedserver(...)

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 7:16 pm
by mtajkov
Avista,

Mogu da Vas kontaktiram putem mail-a?

Pozdrav,
Miloš

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 7:31 pm
by mtajkov
Antonio,

I'm sorry but I do not know how to execute sp_addlinkedserver(...). I looked https://msdn.microsoft.com/en-us/library/ms190479.aspx but I can not understand. Please for more concrete explanation.

Re: connection two SQL server and two databases

PostPosted: Fri May 22, 2015 11:16 pm
by devtuxtla
HI.

This code could help:


cESQUEMA1:="midataone"
nMainone:=DB_oneConnection(cESQUEMA1, cIP)
cESQUEMA2:="midatatwo"
nMaintwo:=DB_oneConnection(cESQUEMA2, cIP)

use "mitablaone" alias a1 shared new via "sqlrdd" connection nMainone
use "mitablatwo" alias a2 shared new via "sqlrdd" connection nMaintwo

select a1
browse()

select a1
browse

a1->(dbclosearea())
a2->(dbclosearea())

return .t.


*--------------------------------------------------------------*
FUNCTION DB_oneConnection(cData, cIP)
*--------------------------------------------------------------*
LOCAL cConn, nConn
DEFAULT cIP:="localhost"
nConn := SR_AddConnection(CONNECT_MYSQL,"MYSQL=" + cIP + ";UID=userone;PWD=passone;DTB=" +cData)
RETURN (nConn)

*--------------------------------------------------------------*
FUNCTION DB_twoConnection(cData, cIP)
*--------------------------------------------------------------*
LOCAL cConn, nConn
DEFAULT cIP:="192.168.1.20"
nConn := SR_AddConnection(CONNECT_MYSQL,"MYSQL=" + cIP + ";UID=usertwo;PWD=passtwo;DTB=" +cData)
RETURN (nConn)

Re: connection two SQL server and two databases

PostPosted: Sat May 23, 2015 5:21 am
by mtajkov
Yes, this works for the opening of the table, but not to query :(

Best regards

Re: connection two SQL server and two databases

PostPosted: Sat May 23, 2015 7:29 am
by mtajkov
Using SQL Manager I execute:

Code: Select all  Expand view
EXEC sp_addlinkedserver
   @server = N'Spedicija',
   @srvproduct = N'',
   @provider = N'SQLNCLI',
   @datasrc = N'192.168.150.2\SqlSmart';
GO


everything works perfectly :D

Best regards