在ASP中处理数据库的NULL记录 - allen_l

[ 2005-06-21 21:46:58 | 作者: Admin ] 字号: | |
在许多时候我们使用ASP+数据库进行网站编程的时候,会遇上读取某记录出来和另外一些变量进行运算的情况,如果取的记录数据为NULL值,那么可能会造成程序返回错误结果,如果每个读记录的语句后都加上判断语句,程序又显得拖沓,因此,我认为编写一个专用的函数对读取的记录进行自动或半自动识别是很好的办法。

根据VBscript的数据类型定义,结合利用VarType函数,构造以下自动处理函数InitdataType,
输入两个参数,要处理数据本身(theDate)和准备输出的数据类型(theReturnType,整数型): theReturnType强制返回的数据类型 ,同VarType返回值定义的意义一样, 'theReturnType 如果忽略: 返回同theDate一样的数据类型.

比较常见的NULL返回,对于字符串型返回空字符串,对各种于数值返回0值,对于逻辑值返回FALSE(假),对于日期

返回最早的日期
函数程序:
Function InitdataType(theDate,theReturnType)
'返回或强制指示变量子类型的值。theDate 参数可以是任何变量。
'theReturnType(整数型): 强制返回的数据类型 ,同VarType返回值定义的意义一样
'theReturnType 如果忽略: 返回同theDate一样的数据类型.

'下面引用的函数VarType(varname)会返回指示变量子类型的值。,varname 参数可以是任何变量。
'VarType函数返回值的意义如下:
'vbEmpty 0 Empty(未初始化)
'vbNull 1 Null(无有效数据)
'vbInteger 2 整数
'vbLong 3 长整数
'vbSingle 4 单精度浮点数
'vbDouble 5 双精度浮点数
'vbCurrency 6 货币
'vbDate 7 日期
'vbString 8 字符串
'vbObject 9 Automation 对象
'vbError 10 错误
'vbBoolean 11 Boolean
'vbVariant 12 Variant(只和变量数组一起使用)
'vbDataObject 13 数据访问对象
'vb???? 14 小数
'vbByte 17 字节
'vbArray 8192 数组

On Error Resume Next
'Err.Clear
dim n_dataType,vo_ReData,vo_renewdata,c_TypeName

n_dataType = VarType(theDate)
'c_TypeName = TypeName(theDate)
If n_dataType<2 then
if isNumeric(theReturnType) then
Select Case theReturnType
case 1
vo_renewdata=NULL
case 2
vo_renewdata=0
case 3
vo_renewdata=0
case 4
vo_renewdata=0
case 5
vo_renewdata=0
case 6
vo_renewdata=0
case 7
vo_renewdata=0
case 8
vo_renewdata=""
case 11
vo_renewdata=DEF_False
case 14
vo_renewdata=0
case 17
vo_renewdata=chr(0)
case else
vo_renewdata=theDate
end Select
else
vo_renewdata=theDate
end if
else
if isNumeric(theReturnType) then
Select Case theReturnType
case 0
vo_renewdata=Empty
case 1
vo_renewdata=NULL
case 2
if isNumeric(theDate) then
vo_renewdata=cInt(theDate)
else
vo_renewdata=Eval("0+" & theDate &"")
if not isNumeric(vo_renewdata) then vo_renewdata=0
end if
case 3
if isNumeric(theDate) then
vo_renewdata=cLng(theDate)
else
vo_renewdata=Eval("0+" & theDate &"")
if not isNumeric(vo_renewdata) then vo_renewdata=0
end if
case 4
if isNumeric(theDate) then
vo_renewdata=cSng(theDate)
else
vo_renewdata=Eval("0+" & theDate &"")
if not isNumeric(vo_renewdata) then vo_renewdata=0
end if
case 5
if isNumeric(theDate) then
vo_renewdata=cDbl(theDate)
else
vo_renewdata=Eval("0+" & theDate &"")
if not isNumeric(vo_renewdata) then vo_renewdata=0
end if
case 6
if isNumeric(theDate) then
vo_renewdata=cCur(theDate)
else
vo_renewdata=0
end if
case 7
if isDate(theDate) then
vo_renewdata=theDate
else
vo_renewdata=cDate(0)
end if
case 8
if Not isNull(theDate) then
vo_renewdata=cStr(theDate)
else
vo_renewdata=""
end if
case 11
If (not isNull(theDate)) or theDate<>"" Then
vo_renewdata=DEF_True
else
vo_renewdata=DEF_False
end if
case 14
if isNumeric(theDate) then
vo_renewdata=cDbl(theDate)
else
vo_renewdata=Eval("0+" & theDate &"")
if not isNumeric(vo_renewdata) then vo_renewdata=0
end if
case 17
if Not isNull(theDate) then
vo_renewdata=CByte(theDate)
else
vo_renewdata=CByte(0)
end if
case else
vo_renewdata=theDate
end Select
else
vo_renewdata=theDate
end if
end if
err.clear
'vMsgBox("rtn:" & cstr(vo_renewdata) & " type:" & cstr(c_TypeName)) & "/" & cstr(n_dataType)
InitdataType=vo_renewdata
End Function

在实际程序中调用示意:
Set conn = Server.CreateObject("ADODB.Connection")
conn.open xDb_Conn_Str
Set rs=Server.Createobject("ADODB.Recordset")

dim n_OD,cNewName ,nNewValue

n_OD=1.2

sql = "SELECT sl_Name,sl_Value FROM Table_SL WHERE sl_name='mike'"
rs.open sql,conn,1
if not rs.eof then
cNewName = InitdataType(rs("sl_Name"),8)
nNewValue = InitdataType(rs("sl_Value"),5) * n_OD
response.write cNewName & "的标准零售价为:" & nNewValue & "(元)"
end if
rs.Close
Set rs=Nothing
Share
评论Feed 评论Feed: http://www.85815.com/feed.asp?q=comment&id=366
UTF-8 Encoding 引用链接: http://www.85815.com/trackback.asp?id=366&key=
这篇日志没有评论.
发表
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:   注册?
验证码 * 请输入验证码