How to check if connection string is valid?

前端 未结 4 1550
旧时难觅i
旧时难觅i 2020-11-28 22:48

I\'m writing an application where a user provides a connection string manually and I\'m wondering if there is any way that I could validate the connection string - I mean ch

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 23:07

    If the goal is validity and not existence, the following will do the trick:

    try
    {
        var conn = new SqlConnection(TxtConnection.Text);
    }
    catch (Exception)
    {
        return false;
    }
    return true;
    

提交回复
热议问题