⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.asp

📁 ajax+asp在线聊天,非常小巧,适合大部分地方使用.
💻 ASP
字号:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
<%Response.Charset="utf-8"%>
<%

'sql_odbc_connection(database,username,password)     'SQL Server数据库用ODBC连接数据库方式

'sql_general_connection(database,username,password)  'SQL Server数据库普通连接方式

'access_connection(database_name)                    'Access数据库连接方式

'close_data_connection()                             '关闭数据库连接

'data_operation(sql)                                 '对数据库里的数据进行删除,修改,添加

'get_data(sql)                                       '从数据库中取出单条数据或取出一组数据

'conversion_content(content)                         '把数据库中的内容转换成有回车,有空格的内容

'pagination(strSql,page_size)                        '分页显示数据





dim conn'把conn声明为全局变量

'调用连接数据库函数
database_name=""
database=""
username=""
password=""
'call sql_odbc_connection(database,username,password)
'call sql_general_connection(database,username,password)
'call access_connection(database_name)

'------------------------------------------------------------------------

'SQL Server数据库用ODBC连接数据库方式
sub sql_odbc_connection(database,username,password)
	set conn=server.createobject("adodb.Connection")
	conn.open "dsn="&database&";uid="&username&";pwd="&password&""
end sub

'------------------------------------------------------------------------

'SQL Server数据库普通连接方式
'(数据库连接初始化)
sub conn_init(database,username,password)                                                               
    connstr = "database="&database&";uid="&username&";pwd="&password&";server=localhost;provider=sqloledb;"
    on error resume next                                                           
    set conn=server.createobject("ADODB.conNECTION")                               
    if err.number<>0 then                                                          
        err.clear                                                                  
        set conn=nothing                                                           
	    response.write "数据库连接出错!"                                          
	    Response.End                                                               
    else                                                                           
	    conn.open connstr                                                          
        if err then                                                                
           err.clear                                                               
           set conn=nothing                                                        
		   response.write "数据库连接出错!!!!"                                   
           Response.End                                                            
        end if                                                                     
    end if                                                                         
end sub                                                                     
'(数据库连接)    
sub sql_general_connection(database,username,password)
    dim conn   
    dim connstr   
    call conn_init(database,username,password)                                                      
end sub

'------------------------------------------------------------------------

'Access数据库连接方式

sub access_connection(database_name)
    connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(database_name)
    set  conn=server.createobject("adodb.connection")
    conn.open connstr
end sub


'------------------------------------------------------------------------

'关闭数据库连接
sub close_data_connection()                                                         
    conn.close                                                                     
    set conn=nothing                                                                                                                       
end sub 

'------------------------------------------------------------------------

'对数据库里的数据进行删除,修改,添加
function data_operation(sql)    
    set rs=Server.CreateObject("ADODB.Recordset")
   
    rs.Open sql,conn,1,3 
    'response.write sql 
    '检测数据操作是否成功
    if conn.errors.count>0 then
        data_operation=0'失败
    else
        data_operation=1'成功
    end if
end function

function data_operation2(sql)   
set rs=Server.CreateObject("ADODB.Recordset") 
    rs.execute sql  
    '检测数据操作是否成功
    if conn.errors.count>0 then
        data_operation=0'失败
    else
        data_operation=1'成功
    end if
end function

'------------------------------------------------------------------------

'从数据库中取出单条数据或取出一组数据
function get_data(sql)   
    set get_data=Server.CreateObject("ADODB.Recordset")    
    get_data.Open sql,conn,1,1  
end function

'------------------------------------------------------------------------

'把数据库中的内容转换成有回车,有空格的内容
function conversion_content(content)
    conversion_content=content
    if conversion_content<>"" then
        conversion_content=replace(conversion_content," ","&nbsp")
        conversion_content=replace(conversion_content,chr(13)+chr(10),"<br>")
    end if
end function

public function my_replace(InputStr) '对单个字符进行轮换
	InputStr=Replace(InputStr,"'",".")
	my_replace=inputstr
end function

public function my_backplace(InputStr) '对单个字符进行轮换
	InputStr=Replace(InputStr,".","'")
	my_backplace=inputstr
end function

'------------------------------------------------------------------------

'分页显示数据
sub pagination(strSql,page_size)
    response.write  "<table width=100%  border=0 cellspacing=0 cellpadding=0>"

    set rs=Server.CreateObject("ADODB.Recordset")
    
    '判断传过来的数是否为空
    pagenum=request.querystring("pagenum")
    if pagenum="" then
	    pagenum=1
    else
	    pagenum=cint(pagenum)
    end if

    rs.CursorType = 1   
    rs.CacheSize = 30 
    rs.Open strSql,conn,1,1
    rs.PageSize = page_size


    '控制下标的范围
    pagecount=rs.pagecount
    if pagenum<1 then pagenum=1
    if pagenum>rs.pagecount then pagenum=pagecount

    if(rs.recordCount<>0)then
       rs.AbsolutePage =pagenum
       '输出各个字段相对应的值
       for j=1 to rs.pagesize

          response.write "<tr><td  height=27>"&rs("title")&"</td></tr>"

           rs.moveNext
          if rs.eof then exit for
       next

       response.write "<tr><td  height=27>"
       '作为分页
        if pagenum=1 then
    	    response.write "上一页"
        else
     	    response.write "<a href=?pagenum="&pagenum-1&">上一页</a>"
       end if
       response.write "&nbsp&nbsp&nbsp"
       if pagenum=pagecount then
        	response.write "下一页"
        else
        	response.write "<a href=?pagenum="&pagenum+1&">下一页</a>"
        end if

      '显示出当前页是总页数的第几页
      response.write "&nbsp&nbsp&nbsp"
      response.write pagenum&"/"&pagecount
      response.write "&nbsp&nbsp&nbsp"
      response.write "</td></tr>"
    end if

    response.write "</table>"

end sub

'----------------------------------------------------------------------------------------------

'介绍FileSystem组件(提示:在返回值是多个或不知是何种类型时用这种方式:set countfile=myfolder.files,然后用:for each fd in countfile)
'set myfile_eob = server.createObject("scripting.FileSystemObject")  '创建一个fileSystem对象

'set myfile = myfile_eob.createTextFile("d:\my.txt")  '创建一个文件,并起名为my.txt

'myfile.writeLine("hello world")  '向文件中写入一行字
'myfile.writeblanklines           '写入一空行

'myfile.fileExists("d:\my.txt")'检测是否存在,返回值是True或False

'set myfile = myfile_eob.openTextFile("d:\my.txt",1,false)  '打开已存在的文件,1表示只读(默认),2表示写,8表示追加

'thisLine = myfile.readline  '读取文件中一行内容
'           myfile.read(n)   '读取文件中n个字符
'           myfile.readAll   '读取文件中所有的内容
'           myfile.skip(n)   '读取文件时,跳过n个字符
'           myfile.skipLine(n)'读取文件时,跳过n行 
           

'AtEndOfLine    '判断当前字符是否处于文件中一行的行末

'AtEndOfStream  '判断当前字符是否处于文件的结尾

'myfile_eob.copyFile sourceFile destinationFile overwrite  'overwrite表示在文件存在的情况下,进行覆盖

'myfile_eob.moveFile sourceFile destinationFile  '移动文件

'myfile_eob.deleteFile fileName  '删除文件

'set newfile = myfile.getFile("d:\my.txt")  '取得文件对象实例

'newfile.dateCreated  '返回文件创建的日期和时间
'newfile.dateLastAccessed  '返回文件上次被使用的日期和时间
'newfile.dateLastModified  '返回文件上次被修改的日期和时间
'newfile.driver            '返回文件所在的驱动器
'newfile.name              '返回文件名
'newfile.parentFloder      '返回文件所在的文件夹
'newfile.path              '返回文件的完整路径
'newfile.size              '返回文件的大小  
'newfile.type              '返回文件的类型

'对文件夹的操作
'myfile_eob.copyFolder soureFile  destinationFile  overwrite  '文件夹的复制
'myfile_eob.createFolder folderName  '创建一个文件夹
'myfile_eob.deleteFolder folderName  '删除一个文件夹
'myfile_eob.moveFolder sourceFile destinationFile  '移动文件夹
'set thisFolder = myfile_eob.getFolder("d:\my")  '创建一个文件夹对象
'thisFolder.files  '返回当前目录下的所有文件(隐藏文件除外)
'thisFolder.isRootFolder  '返回值表示是否为根目录
'thisFolder.parentFolder  '返回上一级目录
'thisFolder.subFolders    '当前目录下所有子目录的集合
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -