MySQL Connection String on Visual FoxPro


If we build an application using Visual FoxPro, using the MySQL database server, then "connection string" is better than ODBC. Why do I say so? If we use "connection string", we no longer need to set the datasource (ODBC) on client computers that will access the MySQL database server. We just install MySQL Connector ODBC that we can download on http://dev.mysql.com/downloads/connector/odbc/


Other advantages using "connection string" is that we simply declare a variable 'PUBLIC' in the initial application or in code ("Command") key only. So if one day our server problems and had to change in another IP we simply replace only one line syntax in that "Command".

Below suppose we have the MySQL database server:

IP Server : 192.168.1.50
MySQL Connector : MySQL ODBC 5.1 Driver
Port : 3306
Database Name : dbTry
User : usrTry
Password : pswdTry

We can declare once a "PUBLIC" variable suppose that the variable name is "MyCon"
PUBLIC MyCon
MyCon="DRIVER={MySQL ODBC 5.1 Driver};SERVER=192.168.1.50;PORT=3306;DATABASE=dbTry; USER=usrTry;PASSWORD=pswdTry;OPTION=3;"
 To use "MyCon" connection variable in any forms, we use a variable suppose "xCon"
xCon = SQLSTRINGCONNECT(MyCon)
IF xCon <= 0
   MESSAGEBOX("Connection Error",0+16, "Error")
ENDIF

msql= "SELECT * FROM Student ;"
IF SQLEXEC(xCon,msql,"cStudent") <= 0
    MESSAGEBOX("Can't Call Student Database",0+16,"Error")
    SQLDISCONNECT(xCon)
    RETURN
ENDIF

SQLDISCONNECT(xCon)
The above command calls the Student Table and accommodated at Cursor "cStudent"

Thus, hopefully useful

8 comments:

  1. AS we say in Brazil : "Show de Bola" that meas you made something special with a soccer ball.
    Ok, We can use VFP standard sql statements insted Mysql to set the "msql" variable?

    Regards
    Fernando Duque

    ReplyDelete
  2. Thanks for sharing. Through this we can connect to mysql and query the database , i have some data entry form in my vfp application using views, if i remove odbc connection from my control panel, getting error as view is created using odbc connection, how to avoid this.
    regards,
    Imran
    if someone can help me reply @ ihsyed@yahoo.com

    ReplyDelete
    Replies
    1. I'm so sorry, because i never using views... i recommend to use Connection String

      Delete
  3. thank you, works like a charm

    ReplyDelete
  4. Thanks for sharing this but I am trying this in a PRG in VFP 9 and can't seem to get it to work. Getting the Connection Error. Connection handle is invalid. Not sure what to try next. Any suggestions?

    Jason...

    ReplyDelete
    Replies
    1. Figured it out. Had the wrong name of the MySQL driver. Had to change it to MySQL ODBC 5.3 ANSI Driver.

      Much thanks.

      Jason...
      jason@westwoodroad.com

      Delete