使用Asp操作数据库
1.通过ODBC或者OLE方式连接的区别?
现在有两种连接数据库的方法。一方面,可以用ODBC产生一个连接,这种连接与任何有ODBC驱动器的数据库(即基本上是市场上所有的数据库)兼容;另一方面,可以用原始OLE DB提供商产生一个连接。
该用哪个提供商?尽可能用原始OLE DB提供商,因为它提供了对数据更有效的访问。Microsoft正逐步用OLE DB取代ODBC标准,应该仅仅在没有原始OLE DB提供商时使用ODBC。
⑴.用ODBC方式连接SQL Server:
①.配置ODBC
②.连接代码:
conn_odbc.asp
<%
Set Conn = Server.CreateObject("ADODB.Connection")
'Conn.Open "DSN=course_dsn;UID=course_user;PWD=course_password;DATABASE=course"
Conn.Open "course_dsn","course_user","course_password"
%>
注意:在配置MyDSN时若指定默认数据库为course则上述代码作用想同,否则第二行的连接方式更有灵活性,可以指定连接某个数据库(当然,前提是course_user对这个数据库有操作权限)。
⑵.用OLE方式连接SQL Server:
conn_ole.asp
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "PROVIDER=SQLOLEDB;DATA SOURCE=10.1.43.238,2433; UID=course_user;PWD=course_password;DATABASE=course"
%>
2.操作数据库:Connection和Recordset
联合使用connection和recordset操作数据库,或者只使用connection操作数据库。
例子:
⑴.联合使用connection和recordset操作数据库
use_db_1.asp
<%
Set conn=Server.CreateObject("ADODB.Connection") '创建连接数据库的对象
conn.Open "course_dsn","course_user","course_password" '使用该对象连接数据库
Set rs=Server.CreateObject("ADODB.RecordSet") '创建记录集对象
rs.Open "select * from user_info",conn,1,1 '使用记录集对象打开数据库
if rs.recordcount>0 then '如果有记录
response.write "User_id User_name<br>"
for i=1 to rs.recordcount '循环读取所有纪录
response.write rs("id")&" "&rs("user_name")&"<br>"
'向浏览器输出纪录的字段
rs.movenext '指针下移一行
if rs.eof then exit for '如果到达记录集底部则退出循环
next
end if
%>
效果:
User_
相关文档:
使地址欄URL固定,隱藏參數:
<head>
</head>
<frameset>
<frame name="" src="Login.aspx" target="Login.aspx" scrolling="auto" noresize></frame>
<nofr ......
1.记录集关闭之前再次打开:
------------------------------------
sql="select * from test"
rs.open sql,conn,1,1
if not rs.eof then
dim myName
myName=rs("name")
end if
sql="select * from myBook"
rs.open sql,conn,1,1
------------------------------------ ......
'creat by qqlxinye@tom.com
'time 2008-09-19
'qq:273453129
'web www.qqlxinye.cn
dim outSmtp,outUser,outPsd,recUser,recSubmit,bodyContent,AddAttachment,ifsend
function init_mail(str1,str2,str3,str4,str5,str6,str7)
outSmtp=str1
outUser=str2
outPsd=str3
recUser=str ......
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- #include file="../configinc/conndb.asp"-->
<!--#include file="../configi ......
1.Asp是Active Server Pages的简称,是解释型的脚本语言环境;
2.Asp的运行需要Windows操作系统,9x下需要安装PWS;而NT/2000/XP则需要安装Internet Information Server(简称IIS);
3.Asp和JSP的脚本标签是“<%%>”,PHP的则可以设定为多种;
4.Asp的注释符号是“'”;
5.使用附加组 ......