各位:
我现在写了如下代码,目的是想检测一下是否成功链接数据库!
出现的问题是:我把SQLServer数据库关闭,它也会出现“数据库链接成功”!
另外还有一个问题是:这个文件要被其它文件引用,如果链接数据库不成功,我想让它“释放”,中止程序向下执行!
1
2'Create a data Connection
3Session.LCID=2052 '中文-中华人民共和国
4Response.Buffer=true
5dim strConnString
6strConnString="Driver={SQL Server}; Database=NewsIssue; Server=(local); uid=sa; pwd="
7dim conn
8set conn=Server.Createobject("adodb.connection")
9conn.Mode=adModeReadWrite
10conn.ConnectionString=strConnString
11if IsObject(conn) then
12conn.open
13Response.write "数据库链接成功"
14else
15response.write "数据库链接失败"
16set conn=Nothing
17Response.end
18end if
---------------------------------------------------------------
第一个问题:应该是缓存的缘故,尝试刷新一下应该就不可以了
第二个问题:可以用on error resume next
---------------------------------------------------------------
1
2'Create a data Connection
3Session.LCID=2052 '中文-中华人民共和国
4Response.Buffer=true
5dim strConnString
6strConnString="Driver={SQL Server}; Database=NewsIssue; Server=(local); uid=sa; pwd="
7dim conn
8set conn=Server.Createobject("adodb.connection")
9conn.Mode=adModeReadWrite
10conn.ConnectionString=strConnString
11conn.open trConnString
12on error resume next
13if err=0 then
14Response.write "数据库链接成功"
15else
16response.write "数据库链接失败"
17end if
18set conn=Nothing
19Response.end
20
说明:至于你八数据库关了跟if IsObject(conn) then
判断语句无关,这句语句只是判断conn是否是对象,既然执行了set conn=Server.Createobject("adodb.connection")
那么,IsObject(conn)必然是真。所以就产生你所说的情况。