为了账号安全,请及时绑定邮箱和手机立即绑定

拆分数据访问并捕获数据以形成

拆分数据访问并捕获数据以形成

C#
回首忆惘然 2021-06-04 14:05:39
在我的项目中,我正在尝试编写易于理解的代码。我目前在一个单独的类中拆分我的数据访问功能。然而,我想要实现的是将错误捕获回我的表单。我目前没有得到这个,我想知道为什么。在我的表单中,我有以下代码:private void btn_Save_ItemClick(object sender, ItemClickEventArgs e){    if (dal.updatePerson(ObjectAfterSaving))    {        MessageBox.Show("Updated!");    }    else    {        MessageBox.Show("error");    };}在我的 dal 对象(派生自DataAccess_Person class)中,我有以下方法:public bool updatePerson(Person p){    conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString);    SqlCommand command = new SqlCommand(@"UPDATE Person    SET PersonName = @PersonName    WHERE PersonID = @PersonID", conn);    command.Parameters.Add("@PersonName", SqlDbType.VarChar).Value = p.Name    {        try        {            if (conn.State == ConnectionState.Closed)            {                conn.Open();            }            int a = command.ExecuteNonQuery();            conn.Close();            if (a > 0)            {                return true;            }            else            {                return false;            }        }        catch (SqlException ex)        {            ex.ToString();            return false;        }    }}我的问题是:让我们说我的方法是否符合要求。我的前端(表单)会显示它(例如 Sql 异常)吗?或者我只会得到“错误”?如果我只会得到错误,我如何改进我的代码以显示异常而不是错误?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信