2

Please tell how to validate dataSource name and PortNumber in Connection String of SqlConnection. Connection state changes to Open, even I donot give any value for dataSource. Like the code below..

var Connection = new SqlConnection("Data Source=;Trusted_Connection=True");
try
{
    Connection.Open();
    MessageBox.Show("Connection Succeeded");
}

My requirement is I need to validate the Data Source name and Port Number that are entered by EndUser.

1
  • If no data source and port number is defined then an SQLException is thrown at Connection.Open, so do you want to validate connection string before opening connection Commented Oct 25, 2013 at 10:54

1 Answer 1

1

For offline validation, use SqlConnectionStringBuilder to parse it...

SqlConnectionStringBuilder myconBuilder = new SqlConnectionStringBuilder();
myconBuilder.ConnectionString = "Data Source=;Trusted_Connection=True";  //Throws exception if garbage connection string like 'abcd' is supplied
if (string.IsNullOrWhiteSpace(myconBuilder.DataSource))
{ 
    //Throw exception that data source specified is blank 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.