如何减少多次尝试捕获

时间:2014-04-05 09:16:55

标签: asp.net

嗨,我是初学者开发一个项目,在我的项目中我使用try catch with try catch,以便我如何专业地编写查询......

我的代码是......

try
    {
        //connection();
        con = new SqlConnection(constr);
        con.Open();
        txtcusname.Text = "";
        txtcusnumber.Text = "";
        query = "sample_SP";
        cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@GetCusID", txtcusid.Text).ToString();
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        GrdCustomerDetails.DataSource = ds;
        GrdCustomerDetails.DataBind();
        con.Close();
        try
        {
            //connection();
            con = new SqlConnection(constr);
            con.Open();
            ViewState["VSCusID"] = txtcusid.Text;
            cmd = new SqlCommand("select contname,mob from CustContacts_TB where cid='" + ViewState["VSCusID"] + " '", con);
            dr = cmd.ExecuteReader();
            dr.Read();
            txtcusname.Text = dr["contname"].ToString();
            txtcusnumber.Text=dr["mob"].ToString();
            con.Close();
        }
        catch(Exception ex)
        {

        }
        finally
        {
            //connection();
            con = new SqlConnection(constr);
            con.Open();
            ViewState["VSCusID"] = txtcusid.Text;
            //cmd = new SqlCommand("select compname from CustCreate_TB inner join CustContacts_TB on CustContacts_TB.'" + ViewState["VSCusID"] + "'=CustCreate_TB.'" + ViewState["VSCusID"] + "' ", con);
            cmd = new SqlCommand("select compname from CustCreate_TB where cid='" + ViewState["VSCusID"] + " ' ", con);
            dr = cmd.ExecuteReader();
            dr.Read();
            txtcompname.Text = dr["compname"].ToString();
            con.Close();
            //txtcusname.DataBind();
        }
    }
    catch (Exception ex)
    { 

    }
    finally
    {
        //connection();
        con = new SqlConnection(constr);
        con.Open();
        cmd = new SqlCommand("select compliantID,priorty,status from NewComp1 where customerid='" + ViewState["VSCusID"] + "' and status='open'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdpending.DataSource = ds;
        grdpending.DataBind();

        cmd = new SqlCommand("select compliantID,priorty,status from NewComp1 where customerid='" + ViewState["VSCusID"] + "' and status='closed'", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        grdClosed.DataSource = ds;
        grdClosed.DataBind();
        con.Close();
    }

可以减少代码并使其格式正确....感谢您的帮助,并且有助于提高我的编码技能

2 个答案:

答案 0 :(得分:0)

你基本上有:

try
    {        
        [block1]
        try
        {
            [block2]
        }
        catch(Exception ex)
        {

        }
        finally
        {
            [block3]
        }
    }
    catch (Exception ex)
    { 

    }
    finally
    {
        [block4]
    }
  }

他们似乎都在获取数据并填充控件或变量。您还没有指定在异常中执行的操作,因此您可以将它们全部放在一个try块中,或者将它们全部放在单独的块中。我不明白为什么他们必须嵌套。 Finally通常用于清理'操作,比如处理开放式连接,但您似乎正在使用Finally进行新选择&填充操作,这是错误的。

这样:

try
    {        
        [block1]
        [block2]
        [block3]
        [block4]      
  }

或者如果您需要知道特定块何时失败但每个都在try / catch

try
  {        
        [block1]                 
  }
  ...
try
  {        
        [block4]                 
  }

答案 1 :(得分:0)

为什么要建立所有这些连接= ???

你可以这样做

try {
using (System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection()) {
    if (cnn.State == ConnectionState.Closed)
        cnn.Open();
    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) {
        //first task
    }
    using (System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand()) {
        //second one
    }
    using (System.Data.SqlClient.SqlCommand cdm2 = new System.Data.SqlClient.SqlCommand()) {
        //third task
    }

}
} catch (SqlClient.SqlException sqlex) {
//catch exception sql
//to do
} catch (Exception ex) {
//generic exceptio
//to do some stuff
} finally {
//if need to do later
}