给出SqlServer的服务名或IP,还有用户名密码如果只是用户名密码错误,瞬间就能返回错误信息而如果输入了一个不存在的SqlServer的话就要等好久啊....我是这么做的
sqlconn = "server=" + SimHA.DatabaseServer + ";uid=" + SimHA.DatabaseUsername + ";pwd=" + SimHA.DatabasePass + ";";            SqlConnection conn = new SqlConnection(sqlconn);
try
            {
                conn.Open();
            }
            catch (Exception err)
            {
                MessageBox.Show("数据库连接失败:" + err.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                conn.Close();
                return false;
            }

解决方案 »

  1.   

    有没有办法在输入了不存在的Server之后还能很快返回错误?最好3秒内
      

  2.   

    在连接字符串中加入这个:
    Connect Timeout=3
      

  3.   

    测试一下SQL SERVER的端口有没有在监听就OK啦。SQL SERVER端口是1433
      

  4.   

    比如先ping一下,不通返回false。
    Connet Timeout短一点,连一次;以后真正执行的时候再改长一点。
      

  5.   

    Connect   Timeout=3我加了这个之后还是老样子哦....
      

  6.   

    benchem 的方法貌似比较可取...还请他老人家为您解答吧...哈哈...
      

  7.   

    select * from sysdatabases where name ='数据库名字'然后判断返回行数,大于0表示有
      

  8.   

    用SOCKET连接SQL服务器的1433或自定义的端口,如果可以连接就代表一般是可以连接到SQL的(不排除第三方程序也占用这个端口)
      

  9.   

    如何测试SQL   SERVER的端口有没有在监听?假设SQL   SERVER端口是1433
      

  10.   

    多线程解决
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Data.SqlClient;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            bool Return=true;
            AutoResetEvent sleepSynchro = new AutoResetEvent(false);
            Exception err = null;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Thread controlThread = new Thread(new ThreadStart(test));
                controlThread.Start();
                if (!sleepSynchro.WaitOne(3000, false))
                {
                    MessageBox.Show("数据库连接超时.", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (!Return)
                {
                    MessageBox.Show("数据库连接失败:" + err.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);             }
            }
            void test()
            {
                string sqlconn = @"Data Source=3.0.0.0;Initial Catalog=eDocument;Integrated Security=True";            SqlConnection conn = new SqlConnection(sqlconn);
                try
                {
                    conn.Open();
                    Return = true;
                }
                catch (Exception e)
                {
                    Return = false;
                    err = e;
                }
                finally
                {
                    conn.Close();
                    sleepSynchro.Set();
                }        }
        }
    }
      

  11.   

    多谢楼上的,有效~不过还是有一点小毛病,当连接超时的时候如果马上退出程序,实际上进程是还在的,在等待controlThread 线程执行完成,即使执行了controlThread.Abort()也没用,一直要等到controlThread 结束了,进程才能真正结束...不知道这点上有没有什么好办法解决?