When connecting to Databases (MSSQL,MySQL,Acces,etc.) using Visual Foxpro. We commonly create database at and remote connection.
But I do not recommend or do that. What do I do is I use connection string.
By doing this you don’t need to create DNS on ODBC.
You just need to install the Database client.
I also do not use Update Table. Table Buffer. etc..
The sample below are much more simple and faster
Example:
1 – Assign Public Variable for your connection String, Below is a sample for MySQL. note: Driver must be assigned twice to let the string work for MySQL
PUBLIC lcDSNLess
lcDSNLess=”DRIVER={MySQL ODBC 5.1 Driver};UID=root;PWD=sa;Database=bill;Server=localhost;DRIVER ={MySQL ODBC 5.1 Driver};PORT=3306;”
2 – (Inquiry) Create Your Connection and Put Mysql Data to a Cursor
STORE SQLSTRINGCONNECT(lcDSNLess,.t.) TO gnConnHandle
IF gnConnHandle < style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">= MESSAGEBOX(‘Cannot make connection’, 16, ‘SQL Connect Error’)
THISFORM.RELEASE
ELSE
**** MYSQL TABLE TO CURSOR(SQLrate) ****
sas= SQLEXEC(gnConnHandle,”SELECT * FROM rates”,’SQLrate’)
= SQLDISCONNECT(gnConnHandle)
ENDIF
3 – (Other Queries Like Update/Insert/Delete )
STORE SQLSTRINGCONNECT(lcDSNLess,.t.) TO gnConnHandle
IF gnConnHandle < style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">= MESSAGEBOX(‘Cannot make connection’, 16, ‘SQL Connect Error’)
THISFORM.RELEASE
ELSE
**** INSERT RECORD ****
sas= SQLEXEC(gnConnHandle,”INSERT INTO rates (type,value1,value2) VALUES (‘R’,150,100) WHERE tag=’R'”)
= SQLDISCONNECT(gnConnHandle)
ENDIF
Hope This Help…
No comments:
Post a Comment