sqlserver读写远程服务器数据前  如何做到先ping是否通。 或其他方式判断,我要存储过程中实现先判断,再读写,否则退出。

解决方案 »

  1.   

    配置好直接读就是了,判断没必要的,ping不通肯定不能读。
      

  2.   

    ping不通数据库很可能能访问的,网络一个设置就可做到这是不同的概念,所以最好的判断是直接连接数据库,搂主说的可能是链接服务器,可以先简单访问,比如如下语句:
    select * from server1.dbname.sys.tables
    把这个语句放在Try中,Catch是否错误,就能检测了
      

  3.   

    不必先判断ping通不通, 同样的目的,直接在begin try ... end try 里去读取,异常处理部分交给begin catch ... end catch 处理.
      

  4.   

    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