请告诉我们如何在SqlConnection的连接字符串中验证dataSource名称和PortNumber。连接状态更改为Open,即使我没有为dataSource提供任何值。像下面的代码..
var Connection = new SqlConnection("Data Source=;Trusted_Connection=True");
try
{
Connection.Open();
MessageBox.Show("Connection Succeeded");
}
我的要求是我需要验证EndUser输入的数据源名称和端口号。
答案 0 :(得分:1)
对于离线验证,请使用SqlConnectionStringBuilder解析它...
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
}