优化SQL语句的函数-赖观配
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


优化SQL语句的函数

发表时间:2007/10/22 12:58:27 评论(0) 浏览(7265)  评论 | 加入收藏 | 复制
   
摘 要:优化SQL语句的函数
正 文:
作者: goodidea
原文地址:http://www.accessoft.com/blog/more.asp?name=goodidea&id=66

Public Function gCombinationString(strText As String, ParamArray Para() As Variant) As String
'2004/07/27 朱彦志(goodidea) myad@yeah.net

'你是否非常讨厌 用 & 一大串的连接变量?
'你是否不得不经常使用""与"""" ?
'就好像这样:
'strFilter = strFilter & " and [" & strFldname & "] like """ & strValue & """"
'
'strSql ="insert into tblpubStyle (Class,Prop,Value,Type,[Set],memo,flag) values ('" & _
' ctl.ControlType & "','" & _
' prp.Name & "','" & _
' prp.Value & "','" & _
' prp.Type & "', " & _
' 2 & "," & _
' "' ', " & _
' false ")"
'
'strCnn = "Provider=sqloledb;" & _
' "Data Source=" & strServerName & ";" & _
' "Initial Catalog=" & strDbname & ";" & _
' "User Id=" & strUid & ";" & _
' "Password=" & strPwd & ";"
'
'用它,gCombinationString,这个十分简单的函数,让冗长的代码更简洁,增强语句可读性
'以下是它的使用典型形式:
'
'strFilter = gCombinationString( "$1 and [$2] like ""$3""",strFilter,strFldname,strValue)
'strCnn= gCombinationString("Provider=sqloledb;Data Source=$1;Initial Catalog=$2;User Id=$3;Password=$4;",_
' strServerName ,strDbname ,strUid, strPwd)
'参数个数不限,参数类型不限
' strSql = gCombinationString("insert into tblpubStyle (Class,Prop,Value,Type,[Set],memo,flag) " & _
' " values ('$1','$2','$3','$4',$5,$6)", _
' ctl.ControlType, prp.Name, prp.Value, prp.Type, 2, False)
'

Const strCharPre = "$"
Dim varItem As Variant
Dim i As Integer

i = 0
For Each varItem In Para
i = i + 1
strText = VBA.Replace(strText, strCharPre & i, varItem)
strText = VBA.Replace(strText, strCharPre & "\", strCharPre)
Next
gCombinationString = strText
End Function

Access软件网交流QQ群(群号:198465573)
 
 相关文章
给我启发的一篇文章:SQL SERVER优化建议  【爱好  2011/8/5】
Sql优化查询速度50个方法小结\SQL Server速度查询优化...  【风行  2012/8/4】
【Access小品】韬光养晦--线型材料开料优化示例  【煮江品茶  2012/8/19】
数据库优化经验分享  【品茶论道  2013/1/12】
关于风行先生的“组合框自动排除选项示例”的优化  【大漠风  2013/2/3】
存储过程编写经验和优化措施  【周芳(转)  2013/9/17】
回味老漫画:《神秘的程序员们》-18:优化选择   【麥田  2014/4/7】
常见问答
技术分类
相关资源
文章搜索
关于作者

赖观配

文章分类

文章存档

友情链接