我有一个表单,它有一些字段并与数据库相关。我正在使用Entity Framework,我想在SQL Server发送错误消息之前处理异常。例如,当用户在数字字段中输入字符串值时,Win或Web应用程序会在SQL Server处理异常之前处理异常。我编写了这段代码,但它并不能处理所有异常。例如,如果字段为空或类型无效,则显示输入字符串不在正确的格式中。
using (var context = new entityTestEntities2())
{
try
{
int stNumber = Convert.ToInt32(textBox2.Text);
var allCustomers = context.setcust(null, stNumber);
}
catch(Exception ex)
{
if (ex.Message.Contains("correct format"))
{
int x= System.Runtime.InteropServices.Marshal.GetExceptionCode();
MessageBox.Show("error number"+x.ToString()+ex.Message);
}
}
}
Integer.TryParse
来从文本框中获取输入。 - OneFineDay