根据主键参数化查询一个字段值的ASP数据库操作函数


关键词

主键 查询字段 ASP函数 参数化查询

摘要

本文介绍一个ASP操作数据库的函数,它可以直接通过一个主键值来获取数据库表中某一个字段的值。这个函数很安全。

这个ASP操作数据库的具体内容如下:

'获取数据库信息,1个结果字段,1个条件
Function GetDbData11(tbName,col,condition,conditionType,conditionValue)
    GetDbTableInfo = ""    
    Dim connT,cmdT,rsT,i
    Set connT=Server.CreateObject("ADODB.Connection")
    Set cmdT=Server.CreateObject("ADODB.Command")
    connT.open dbstr
    With cmdT    
    Set .ActiveConnection = connT
    .CommandType = 1
    .CommandText = "select top 1 " & col & " from " & tbName & " where " & whereState
    For i=1 To UBound(whereArray)
        If whereArray(i,2) = "int" Then
            .Parameters.Append .CreateParameter(whereArray(i,1),3,1,4,whereArray(i,3))
        Else
            .Parameters.Append .CreateParameter(whereArray(i,1),200,1,128,whereArray(i,3))
        End If
    Next
    set rsT=.Execute()
    End With
    Set cmdT.ActiveConnection = Nothing
    set cmdT=nothing
    if not rsT.eof then GetDbTableInfo2=rsT(col)
    rsT.close
    connT.close
End Function

解释一下GetDbData11这几个参数的含义:
tbName:数据库表名
col:要查询的字段名
condition:条件名称,即主键的字段名
conditionType:条件的类型,即主键字段的类型,如果是整数就填int,如果是字符串,就填varchar
conditionValue:条件的值,即主键的值

使用举例:
如果有一个用户表(表名为tb_user),有user_id、user_name、user_right等多个字段。这时我想通过user_id来获取user_right,可以怎么通过这个asp数据库操作函数来实现呢?

user_id = 10 '已知user_id=10
Dim user_right
user_right = GetDbData11("tb_user", "user_right", "user_id", "int", user_id)

关于根据主键参数化查询一个字段值的ASP数据库操作函数,本文就介绍这么多,希望对您有所帮助,谢谢!

 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权


如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:8771947!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« ASP将数据库中的表获取到数组中 根据主键参数化查询多个字段值的ASP数据库操作函数 »

相关文章: