本文主要是介绍VFP+SQL编程方法讨论与经验分享,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://community.csdn.net/Expert/topic/4126/4126818.xml?temp=6.805056E-02
VFP是一个简单实用的编程工具,但数据库一般用其本身的DBF数据库,对于SQL可能每个人都用不同的看法,各位高手有什么好的方法、经验、建议全都拿出来,供大家分享,谢谢!
以下是我平时对SQL的使用点滴,欢迎各位指点!
**************************************************************
*-- vfpsql
*-- parameters :tcDatabase,tcServer,tcUserId,tcPassword
*-- Sql Connect
**************************************************************
FUNCTION VFPSql()
PARAMETERS tcDatabase,tcServer,tcUserId,tcPassword
SET MULTILOCKS ON
LOCAL lcSqlConnectStr
PUBLIC gcSqlConnectstr
IF TYPE("gcSqlConnectstr")<>"C"
gcSqlConnectstr = ""
ENDIF
lcSqlConnectStr =''
IF TYPE("_gnSqlConnectHandle") = "N" AND _gnSqlConnectHandle > 0
disVFPSql()
ENDIF
PUBLIC _gnSqlConnectHandle
_gnSqlConnectHandle =0
IF PARAMETERS()>=3 AND VARTYPE(tcDatabase)='C' AND VARTYPE(tcServer)='C' AND VARTYPE(tcUserId)='C'
IF EMPTY(tcPassword)
tcPassword = ''
ENDIF
lcSqlConnectStr ="driver={Sql Server};Database=&tcDatabase;server=&tcServer;uid=&tcUserId;pwd=&tcPassword"
_gnSqlConnectHandle = SQLSTRINGCONNECT(lcSqlConnectStr)
ELSE
IF gcSqlConnectstr <> ""
lcSqlConnectStr = gcSqlConnectstr
_gnSqlConnectHandle = SQLSTRINGCONNECT(lcSqlConnectStr)
ENDIF
ENDIF
gcSqlConnectstr = lcSqlConnectStr
LOCAL iLoopCount
iLoopCount = 1
DO WHILE _gnSqlConnectHandle <= 0 AND iLoopCount <=3
DO prgDataConnect
iLoopCount = iLoopCount + 1
ENDDO
RETURN _gnSqlConnectHandle>0
*- VFPSQL END
************************************************
*-- disVFPSql()
*-- Sql Disconnect
************************************************
FUNCTION disVFPSql()
IF TYPE("_gnSqlConnectHandle")<>'N' OR _gnSqlConnectHandle<=0
Return
ENDIF
SQLDISCONNECT(_gnSqlConnectHandle)
RELEASE _gnSqlConnectHandle
RETURN
*- DisVFPSql END
***************************************
*-- SqlDo()
*-- PARAMETERS tcSqlString: 查询语句
*--tcCurName : 结果集存放表
*- 功能: 执行查询
**************************************
FUNCTION SqlDo
LPARAMETERS tcSqlString,tcCurName
LOCAL lnReturn
*-- 检查连接
IF TYPE("_gnSqlConnectHandle")<>"N" or _gnSqlConnectHandle<=0
IF !vfpsql()
*-- 连接失败,返回 0
RETURN 0
ENDIF
ENDIF
IF TYPE('tcSqlString')<>'C' OR EMPTY(tcSqlString)
RETURN 0
ENDIF
IF EMPTY(tcCurName) OR TYPE("tcCurName") <>'C'
lnReturn = SQLEXEC(_gnSqlConnectHandle,tcSqlString)
ELSE
lnReturn = SQLEXEC(_gnSqlConnectHandle,tcSqlString,tcCurName)
ENDIF
RETURN lnReturn
*- SqlDo END
*-- Opendb
********************************************************
*-- OpenDB
*
*- 功能:打开table Or View
*
*-参数说明 tcCurName : 临时表名
*- tcSqlTblName: Sql数据表名(默认tcCurName)
* tcFldsList : 字段列表(默认为全部 *)
**********************************************************
FUNCTION OpenDB
LPARAMETERS tcCurName,tcSqlTblName,tcFldsList
IF VARTYPE(tcCurName)<>"C"
MESSAGEBOX("参数错误,不能打开数据表或视图!",48,"错误")
RETURN .F.
ENDIF
IF EMPTY(tcSqlTblName)
tcSqlTblName = tcCurName
ENDIF
IF EMPTY(tcFldsList)
tcFldsList = "*"
ENDIF
*-- 执行查询
lnReturn = Sqldo("select &tcFldsList From &tcSqlTblName ","&tcCurName.")
*-- 打开开放式表缓冲
IF lnReturn > 0
cursorsetprop("buffering",5, "&tcCurName.")
ENDIF
RETURN lnReturn>0
*-- OpenDB END
********************************************************
*-- CloseDB
*
*- 功能:关闭table Or View
*
*-参数说明 tcCurName : 临时表名
*-
**********************************************************
FUNCTION CloseDB
LPARAMETERS tcCurName
IF VARTYPE(tcCurName)<>"C"
MESSAGEBOX("参数错误,非法数据表或视图!",48,"错误")
RETURN .F.
ENDIF
IF SELECT(tcCurName) > 0
USE IN &tcCurName
ENDIF
RETURN .T.
*-- CloseDB END
**********************************************************************
*- SaveData
*--
*-- 参数说明 tcCurName : 临时表名
*-- tcSqlTblName: Sql数据表名(默认tcCurName)
*-- tcKeyFld : 关键字段名称(一定要有,否则默认为ID),
*-- 程序要根据该字段来定位记录,
*-- 功能:从临时表保存到 SQL数据库中,注意,临时表要启用缓冲模式
**********************************************************************
FUNCTION savedata
LPARAMETERS tcCurName,tcSqlTblName,tcKeyFld
LOCAL lcFldList,lcFldListTmp,lnNextModifyRec,lcFldValue,lnFldCnt
LOCAL lcSqlString,lnSqlReturn
LOCAL lcOldAlias,lcOldDele,lcOldSafe
LOCAL laFlds(1)
LOCAL i,llNeedAddDH
IF TYPE("tcCurName")<>'C' OR ;
EMPTY(tcCurName) OR ;
SELECT(tcCurName)=0
MESSAGEBOX("表名错误!",48,"错误提示")
RETURN
ENDIF
lcOldDele = SET("Deleted")
lcOldSafe = SET("Safety")
SET DELETED OFF
SET SAFETY OFF
lcOldAlias = ALIAS()
SELECT (tcCurName)
IF cursorgetprop('buffering')=1
MESSAGEBOX("数据表没有启用缓冲模式,程序无法识别!",48,"错误提示")
RETURN .F.
ENDIF
IF EMPTY(tcSqlTblName) OR TYPE("tcSqlTblName")<>'C'
tcSqlTblName = tcCurName
ENDIF
IF EMPTY(tcKeyFld)
tcKeyFld = 'Id'
ENDIF
lnFldCnt = AFIELDS(laFlds)
lcFldList = ''
FOR i = 1 TO lnFldCnt
IF !EMPTY(lcFldList)
lcFldList = lcFldList +','
ENDIF
lcFldList = lcFldList + laFlds(i,1)
ENDFOR
lnNextModifyRec = GETNEXTMODIFIED(0)
lnSqlReturn = SQLDO("BEGIN TRANSACTION")
IF lnSqlReturn =-1
RETURN .F.
ENDIF
DO WHILE lnNextModifyRec <>0
GO lnNextModifyRec
lcFldListtmp = ''
lcSqlString = ''
DO case
CASE DELETED()&&删除
lcSqlString = "Delete From "+tcSqlTblName + IIF(TYPE(tcKeyFld) $ "NI",;
" Where &tcKeyFld. = "+TRANSFORM(EVALUATE(tcCurName+".&tcKeyFld.")),;
" Where &tcKeyFld. = '"+TRANSFORM(EVALUATE(tcCurName+".&tcKeyFld."))+"'")
CASE RECNO()<0 &&新增
lcSqlString = "" &&"insert into "+tcSqlTblName +" ("+lcFldList+") Values ("
FOR i = 1 TO lnFldCnt
lcFldValue = EVALUATE(tcCurName+'.'+laFlds(i,1))
IF EMPTY(lcFldValue)
Loop
ENDIF
IF !EMPTY(lcFldListtmp)
lcFldListtmp = lcFldListtmp +','
ENDIF
lcFldListtmp = lcFldListtmp + laFlds(i,1)
DO case
CASE laFlds(i,2)$'NI'
lcFldValue = TRANSFORM(lcFldValue)
CASE laFlds(i,2)='L'
IF lcFldValue
lcfldValue = '1'
ELSE
lcfldValue = '0'
ENDIF
OTHERWISE
lcFldValue ="'"+ TRANSFORM(lcFldValue)+"'"
ENDCASE
IF !EMPTY(lcSqlString)
lcSqlString =lcSqlString +','
ENDIF
lcSqlString =lcSqlString + lcFldValue
ENDFOR
lcSqlString ="insert into "+tcSqlTblName +" ("+lcFldListtmp+") Values ("+lcSqlString +')'
OTHERWISE
lcSqlString = "Update "+tcSqlTblName +" set "
llNeedAddDH = .F.
FOR i = 1 TO lnFldCnt
IF GETFLDSTATE(i)<>2
LOOP
ENDIF
DO case
CASE laFlds(i,2)$'NI'
lcFldValue = laflds(i,1)+'='+TRANSFORM(EVALUATE(tcCurName+'.'+laFlds(i,1)))
CASE laFlds(i,2)='L'
IF EVALUATE(tcCursorName+'.'+laFlds(i,1))
lcfldValue = laflds(i,1)+'=1'
ELSE
lcfldValue = laflds(i,1)+'=0'
ENDIF
OTHERWISE
lcFldValue =laflds(i,1)+" = '"+ TRANSFORM(EVALUATE(tcCurName+'.'+laFlds(i,1)))+"'"
ENDCASE
IF NOT llNeedAddDH
llNeedAddDH = .T.
Else
lcSqlString =lcSqlString +','
ENDIF
lcSqlString =lcSqlString + lcFldValue
ENDFOR
lcSqlString =lcSqlString + IIF(TYPE(tcKeyFld) $ "NI",;
" Where &tcKeyFld. = "+TRANSFORM(EVALUATE(tcCurName+".&tcKeyFld.")),;
" Where &tcKeyFld. = '"+TRANSFORM(EVALUATE(tcCurName+".&tcKeyFld."))+"'")
ENDCASE
lnSqlReturn = sqlDo(lcSqlString)
IF lnSqlReturn=-1
SQLDO("Rollback")
MESSAGEBOX("保存失败!"+CHR(13)+CHR(10)+lcSqlString)
_cliptext = lcSqlstring
TABLEREVERT(.T.)
RETURN .F.
ENDIF
lnNextModifyRec = GETNEXTMODIFIED(lnNextModifyRec)
ENDDO
lnSqlReturn = SQLDO(" COMMIT TRANSACTION ")
TABLEUPDATE(.T.)
IF !EMPTY(lcOldAlias) AND ALIAS()<>lcOldAlias
SELECT (lcOldAlias)
ENDIF
IF lcOldDele='ON'
SET DELETED on
ENDIF
IF lcOldSafe = 'ON'
SET SAFETY on
ENDIF
RETURN .T.
*-- 调用SQL设置
PROCEDURE prgdataconnect
LOCAL lcDataPath,lcTextString
LOCAL loForm
_gnSqlConnectHandle = 0
IF _gnSqlConnectHandle <=0
loFormCon = CREATEOBJECT("fmConnect")
loFormCon.Show(1)
ENDIF
**********************************************
*- 定义连接表单
*********************************************
DEFINE CLASS fmConnect as Form
&&-"driver={Sql Server};Database=Hjasset;server=jianqy;uid=sa;pwd=")
Caption = 'SQL服务器连接'
Width = 300
Height = 182
BorderStyle = 2
maxbutton = .F.
minButton = .F.
ShowWindow = 1
Autocenter = .T.
alwaysontop = .T.
desktop = .T.
windowType = 1
Name = 'fmConnect'
ADD OBJECT lblServer as label WITH Caption = '服务器:',;
left = 15,;
Width = 80,;
Top = 20,;
Height= 22,;
Alignment = 1,;
BackStyle = 0,;
Visible = .T.
ADD OBJECT txtServer as textbox WITH left = 95,;
Width = 160,;
Top = 17,;
Height= 25,;
Value = '(Local)',;
Visible = .T.
ADD OBJECT lblDatabase as label WITH Caption = '数据库:',;
left = 15,;
Width = 80,;
Top = 50,;
Height= 22,;
Alignment = 1,;
BackStyle = 0,;
Visible = .T.
ADD OBJECT txtDatabase as textbox WITH left = 95,;
Width = 160,;
Top = 47,;
Height= 22,;
Value = 'pubs',;
Visible = .T.
ADD OBJECT lblUid as label WITH Caption = '用户名:',;
left = 15,;
Width = 80,;
Top = 80,;
Height= 22,;
Alignment = 1,;
BackStyle = 0,;
Visible = .T.
ADD OBJECT txtUid as textbox WITH left = 95,;
Width = 160,;
Top = 77,;
Height= 22,;
Value = 'sa',;
Visible = .T.
ADD OBJECT lblPwd as label WITH Caption = '密 码:',;
left = 15,;
Width = 80,;
Top = 110,;
Height= 22,;
Alignment = 1,;
BackStyle = 0,;
Visible = .T.
ADD OBJECT txtPwd as textbox WITH left = 95,;
Width = 160,;
Top = 107,;
Height= 22,;
passwordchar= '*',;
Value = '',;
Visible = .T.
ADD OBJECT btnConnect as Commandbutton WITH left = 35,;
Width = 100,;
Top = 150,;
Height= 22,;
Value = '',;
Visible = .T.,;
Caption = '连 接'
ADD OBJECT btnCancel as Commandbutton WITH left = 165,;
Width = 100,;
Top = 150,;
Height= 22,;
Value = '',;
Visible = .T.,;
Caption = '取 消'
ADD OBJECT cntShp1 as Container WITH left = 0,;
Width = 300,;
Top = 142,;
Height= 1,;
Visible = .T.,;
SpecialEffect = 1
PROCEDURE destroy
PROCEDURE load
PROCEDURE init
PROCEDURE txtPwd.Keypress
LPARAMETERS nKeyCode, nShiftAltCtrl
IF nKeyCode = 3 OR nKeyCode = 24
NODEFAULT
ENDIF
PROCEDURE btnConnect.click
LOCAL lcServer,lcDatabase,lcUid,lcPwd,lcSqlStrConnect
lcServer = ALLTRIM(thisform.txtServer.Value)
lcDatabase = ALLTRIM(thisform.txtDatabase.Value)
lcUid = ALLTRIM(thisform.txtUid.Value)
lcPwd = ALLTRIM(thisform.txtPwd.Value)
IF EMPTY(lcServer)
MESSAGEBOX("请输入SQL服务器的名称!",48,"系统提示")
thisform.txtServer.setfocus
RETURN
ENDIF
lcSqlStrConnect="driver={Sql Server};"+;
IIF(EMPTY(lcDatabase),'',"Database="+lcDatabase+";")+;
"server="+lcServer+";uid="+lcUid+";pwd="+lcPwd
_gnSqlConnectHandle = SQLSTRINGCONNECT(lcSqlStrConnect)
gcSqlConnectstr = lcSqlStrConnect
ON ERROR
IF _gnSqlConnectHandle > 0
MESSAGEBOX("连接成功!",64,"SQL连接")
thisform.Release
ELSE
MESSAGEBOX("连接失败!"+CHR(13)+CHR(10)+"请重新输入连接项",64,"SQL连接")
thisform.txtServer.setfocus
ENDIF
PROCEDURE btnCancel.click
thisform.Releas*-- 以pubs 为例
&&连接SQL
vfpsql()
&&打开authors
opendb("authors")
&&修改
repl au_lname with 'dfjkdjfkdj' for au_id = '722-51-5454'
&&保存到SQL
savedata("authors","authors","au_id")
一、采用ado方式,可能代码会很多,但是会很灵活,而且可以让前台没有dbc文件了,如果采用远程视图方式,就不会写这么多代码了,但是必须要定义DBC(数据库)用于保存远程视图及连接等,前一种方式,楼主已经写的比较通俗简明了,我对后一种简单补充一下。
1.建立数据库,然后建立与后台数据库的连接(当然可以用INI文件等先保存定义,然后动态设定)
2.建立远程视图表
3.打开表同时设置表的缓冲级别,一般为行缓冲和表缓冲
4.修改后如果保存=tableupdate(.t.),放弃=tablerevert(.t.)
5.可以通过捕获返回的错误信息,显示给用户.如
IF TABLEUPDATE(.T.)
=MESSAGEBOX("更新完毕!",48,"系统信息")
ELSE
=AERROR(ERR)
IF !EMPTY(ERR(2))
=SHOWWARN("错误信息:"+ALLTRIM(SUBSTR(UPPER(ERR(2)),AT('[SQL SERVER]',UPPER(ERR(2)),1)+12)))
ENDIF
XX=SHOWCHOICE("是否取消此次操作?")
IF XX=1
=TABLEREVERT(.T.)
ELSE
RETURN
ENDIF
ENDIF
=REQUERY()
二、当然了,现在最好使用的还是CursorAdapter方式,它即有了ado方式的灵活,又有了远程视图的简易,还有了后台升级方便等优点。具体使用请参考网上的一些说明,在些从简了。
ENDDEFINE
这篇关于VFP+SQL编程方法讨论与经验分享的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!