如果你是用DSN 连接数据库的话,当你打开网页时页面显示以下提示:
2 h U5 l$ C! T; M, h 错误类型: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
; S) Z9 Z7 L6 r* ]( K" D/ C . J7 ~7 S* } V3 b; x
这个原因最大的可能是:-------你的DSN没有做好! * C3 _" ]3 @3 d8 {4 Z: Z5 c& `
建立DSN时有三个选择:用户DSN,系统DSN ,和文件DSN ,如果你在用户DSN中做好DSN 连接了,仍发现出现上述问题,那就换成在系统DSN中做一个,就好用了
/ z3 t/ G4 @0 {6 o% ~: j+ ]! Z 另外,在建立DSN的最后一步的对话框中,左下角有个“测试连接”的按钮,可以点击测试一下建立的DSN是否连接成功 6 d; A) t* M; A7 g4 G
在系统中设置好DSN后,还要在页面中正确书写连接数据库的代码 以下是在ASP网页中连接数据库的代码(DSN的方法)
! R1 W2 K5 E& T3 K" d/ r( r# l7 h+ N<table width=100%> <% & C; [: V4 n0 O
dim conn ,studentRs set conn=Server.Createobject("Adodb.connection") set studentRs=Server.Createobject ("ADODB.Recordset") conn.open "DSN=student;UID=xiaojian;PWD=xiaojian" studentRs.open "select * from zhaoshang",conn do while not studentRs.eof response.write "<tr>" response.write "<td>" & studentRs.Fields("name") &"</td>" response.write "<td>" & studentRs.Fields("people") &"</td>"
- h9 r2 Z! i, X& e response.write "</tr>" studentRs.movenext loop studentRs.close set studentRs=nothing conn.Close set conn=nothing %> </table> 5 e) L0 K' g- e3 j9 ]8 C7 y
( b& s6 z! K) ~( ]( g8 _/ @ 以上如果还不能解决问题的话,那干脆就不要用DSN连接了吧,用非DSN连接,不经过系统设置,只在ASP页面中直接模仿添加以下代码就可以了: 3 J2 j. H v0 u7 C
1.sql数据库连接方法
0 r0 V( Y! ~5 T$ j0 F# ^; p. f <table width=100%> <% dim conn dim connstr dim db,pass_word,User_ID,Data_Source db="mylove" '数据库名称 Pass_word="xiaojian" 'Password=帐号密码 User_ID="xiaojian" 'User ID=登陆帐号 Data_Source="(local)" 'Data Source=服务名称或者ip connStr="Provider=SQLOLEDB.1;Password='"&pass_word&"';Persist Security Info=True;User ID='"&User_ID&"';Initial Catalog='"&db&"';Data Source='"&Data_Source&"'" set conn=server.createobject("ADODB.CONNECTION") conn.open connstr
3 G4 ?9 G0 m: r e, j set studentRs=Server.Createobject ("ADODB.Recordset") studentRs.open "select * from zhaoshang",conn do while not studentRs.eof response.write "<tr>" response.write "<td>" & studentRs.Fields("name") &"</td>" response.write "<td>" & studentRs.Fields("people") &"</td>" ) }5 i0 G7 f6 x5 b3 D
response.write "</tr>" studentRs.movenext loop studentRs.close set studentRs=nothing conn.Close set conn=nothing %> </table> * v* r% v5 ]8 F# o$ @( O* m
2.Access数据库连接方法 ' ~" c E+ Z* j- W! S; M
<% dim conn,connstr,time1,time2 time1=timer on error resume next
# L0 T! v& f7 c. EConnstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=D:/wwwroot/leizhentian/databases/mylove.asp" Set Conn=Server.CreateObject("ADODB.Connection") conn.Open connstr 5 ~& ^, A( N# E$ s
If Err Then err.Clear Set Conn = Nothing Response.Write "数据库连接出错,请检查连接字串。" Response.End End If sql="Select * FROM minglu where d_id=1 order by id desc" set rs=server.createobject("adodb.recordset") rs.open sql,conn,1,1 do while not rs.eof response.write rs("name") rs.movenext loop
5 a, p/ o; S4 q' b8 x) e ors.close set rs=nothing
% E. J5 P& z7 [$ `2 t* `%>
: ~; j$ ^) u8 }6 o( @3。总结 + G$ N) D" I4 k! A+ i7 e2 D
DSN连接方法可以连接access和sql数据库且方法一样,即DSN连接方法不区分数据库类型
; f; t( m4 C0 x# y4 b+ k相反,非DSN连接方法区分数据库类型
" @. h1 H% I# n8 c5 P: \; ~" q5 maccess和sql数据库连接的不同之处: 9 L" h; f0 ~: X2 Y
access需要指定数据库路径,而sql不需要指定数据库路径
0 a& Z- Q# V) l. ?% w/ U 这一点虽然简单,但在学习了access数据库后初次接触sql数据库时总觉的应当加上路径什么的,感觉怪怪的,并且由于浅意识里始终认为应当指名数据库路径,所以看了很多sql连接数据库的方法总看不明白,总以为应该在sql服务器上设置些什么,呵呵,浪费了太多的时间了 |