这种情况下是不会出现ArgumentException 的。ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. All instances of ArgumentException should carry a meaningful error message describing the invalid argument, as well as the expected range of values for the argument.也就是说, 只有当至少一个参数不满足被调用方法的参数定义时,才会出错。这里ConnectionString要求是一个字符串,你提供的也是一个字符串,虽然不正确,但是却满足对参数的定义。 这里只会出 SqlException.
那不是ArgumentException吧??
试试.
myconnect.ConnectionString = "S";
try
{
myconnect.Open();
}
catch(ArgumentException e)
{
MessageBox.Show(e.Message.ToString());
}
catch(Exception e)
{
MessageBox.Show(e.GetType().FullName);
}
myconnect.ConnectionString = "S";请将此句包含在try中即可
try
{
myconnect.ConnectionString = "S";
myconnect.Open();
}
catch(ArgumentException e)
{
MessageBox.Show(e.Message.ToString());
}
catch(Exception e)
{
MessageBox.Show(e.Message.ToString());
}这样可以了吧? ;)