ping不通数据库很可能能访问的,网络一个设置就可做到这是不同的概念,所以最好的判断是直接连接数据库,搂主说的可能是链接服务器,可以先简单访问,比如如下语句: select * from server1.dbname.sys.tables 把这个语句放在Try中,Catch是否错误,就能检测了
不必先判断ping通不通, 同样的目的,直接在begin try ... end try 里去读取,异常处理部分交给begin catch ... end catch 处理.
create table #t(info nvarchar(200)) insert into #t EXEC master..xp_cmdshell 'ping -n 1 192.168.1.1' go select * from #t /* info -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Pinging 192.168.1.1 with 32 bytes of data:Reply from 192.168.1.1: bytes=32 time<1ms TTL=64Ping statistics for 192.168.1.1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0msNULL(10 行受影响)*/ drop table #t
select * from server1.dbname.sys.tables
把这个语句放在Try中,Catch是否错误,就能检测了
insert into #t
EXEC master..xp_cmdshell 'ping -n 1 192.168.1.1'
go
select * from #t
/*
info
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pinging 192.168.1.1 with 32 bytes of data:Reply from 192.168.1.1: bytes=32 time<1ms TTL=64Ping statistics for 192.168.1.1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0msNULL(10 行受影响)*/
drop table #t