昨天做的一个ASP操作数据库的函数库

类别:Asp 点击:0 评论:0 推荐:

<%

 '*======================================
 '* 名称:useDb.asp
 '* 功能:数据库操作函数库
 '* 作者:intereye
 '* 信箱:[email protected]
 '* 主页:http://www.inteye.net
 '* BLOG:http://blog.csdn.net/intereye
 '*======================================

 '* 函数:openDb(dbType,dbUser,dbPass,dbName,dbServer,dbPath)
 '* 功能:打开数据库连接
 '* 参数:dbType->数据库类型 MDB ACCESS数据库 || SQLSERVER SQLSERVER数据库
 '* 参数:dbUser->访问数据库用户名
 '* 参数:dbPass->访问数据库密码
 '* 参数:dbName->数据库名称
 '* 参数:dbServer->数据库Host
 '* 参数:dbPath->数据库路径
 
 Function openDb(dbType,dbUser,dbPass,dbName,dbServer,dbPath)
  Dim Conn
  Set Conn = Server.CreateObject("ADODB.Connection")
  Select case dbType
   case "MDB":
    connStr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath(""&dbPath&dbName&"")
   case "SQLSERVER":
    connStr = "Provider=SQLOLEDB.1;Password="&dbPass&";Persist Security Info=True;User ID="&dbUser&";Initial Catalog="&dbName&";Data Source="&dbServer&""
  End Select
  Conn.Open connStr
 End Function
 
 '* 函数:add(tabname,fieldlist,dblist)
 '* 功能:在数据库中插入一条记录
 '* 参数:tabname->数据表名
 '* 参数:dblist->数据表字段名数组
 '* 参数:fieldlist->表单变量名数组
 '* 返回:0 false || 1 true
 
 Function add(tabname,dblist,fieldlist)
  Sql = "INSERT INTO "&tabname&"("
  Value = ""
  Field = ""
  For Each v in dblist
   Field = Field & v & ","
  Next
  Field = Left(Field,Len(Field)-1)
  Value = Field & ") VALUES("
  For Each v in fieldlist
   If Request.Form(v) <> "" Then
    Value = Value & "'" & Request.Form(v) & "',"
   Else
    Value = Value & "'" & v & "',"
   End If
  Next
  Value = Left(Value,Len(Value)-1)
  Sql = Sql & Value & ")"
  Conn.Execute(Sql)
  CloseDb()
  If Err Then
   add = 0
  Else
   add = 1
  End If
 End Function
 
 '* 函数:update(tabname,dblist,fieldlist,id)
 '* 功能:更新数据库中指定的一条记录
 '* 参数:tabname->数据表名
 '* 参数:dblist->数据库字段名称数组
 '* 参数:fieldlist->表单变量名数组
 '* 参数:id->数据ID号
 '* 返回:0 false || 1 true
 
 Function update(tabname,dblist,fieldlist,id)
  Sql = "UPDATE " & tabname & " Set "
  Value = ""
  For i=0 to ubound(dblist)
   Value = Value & dblist(i) & "='"
   If Request.Form(fieldlist(i)) <> "" Then
    Value = Value & Request.Form(fieldlist(i)) & "',"
   Else
    Value = Value & fieldlist(i) & "',"
   End If
  Next
  Value = Left(Value,Len(Value)-1)
  Sql = Sql & Value & " WHERE id=" & id
  Conn.Execute(Sql)
  CloseDb()
  If Err Then
   update = 0
  Else
   update = 1
  End If  
 End Function
 
 '* 函数:del(tabname,id)
 '* 功能:从数据库中删除一条指定记录
 '* 参数:tabname->数据表名称
 '* 参数:id->数据ID号
 '* 返回:0 false || 1 true
 
 Function del(tabname,id)
  Sql = "DELETE FROM " & tabname & " WHERE id in(" & id & ")"
  Conn.Execute(Sql)
  CloseDb()
  If Err Then
   del = 0
  Else
   del = 1
  End If
 End Function
 
 '* 函数:getRow(tabname,fieldlist,caseStr)
 '* 功能:从数据库中取得一行
 '* 参数:tabname->数据表名
 '* 参数:fieldlist->数据字段数组
 '* 参数:caseStr->Sql条件语句
 
 Function getRow(tabname,fieldlist,caseStr)
  If Not isArray(fieldlist) Then
   fieldlist = "*"
  Else
   Field = ""
   For Each val in fieldlist
    Field = Field & val & ","
   Next
   fieldlist = Left(Field,Len(Field)-1)
  End If
  Sql = "SELECT " & fieldlist & " FROM " & tabname & caseStr
  Set Rs = Conn.Execute(Sql)
  If Rs.Eof AND Rs.Bof Then
   getRow = 0
  Else
   getRow = 1
  End If
 End Function
 
 '* 函数:CloseDb()
 '* 功能:关闭数据库连接并释放对象

 Function CloseDb()
  Conn.Close
  Set Conn = Nothing
 End Function
%>

本文地址:http://com.8s8s.com/it/it8012.htm