Asp.net 如何减少多次尝试捕获
嗨,我是一个开发项目的初学者,在我的项目中我使用了try-catch和try-catch,所以我如何专业地编写查询 我的代码是Asp.net 如何减少多次尝试捕获,asp.net,Asp.net,嗨,我是一个开发项目的初学者,在我的项目中我使用了try-catch和try-catch,所以我如何专业地编写查询 我的代码是 try { //connection(); con = new SqlConnection(constr); con.Open(); txtcusname.Text = ""; txtcusnumber.Text = ""; query = "sample_SP";
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();
}
它可以减少代码和它的正确格式。。。。感谢您的帮助,这对培养我的编码技能很有帮助您基本上具备:
try
{
[block1]
try
{
[block2]
}
catch(Exception ex)
{
}
finally
{
[block3]
}
}
catch (Exception ex)
{
}
finally
{
[block4]
}
}
它们似乎都在获取数据并填充控件或变量。您没有指定在异常中要执行的操作,因此您可以简单地将它们全部放在单个try块中,或者将它们全部放在单个块中。我不明白为什么它们必须嵌套。
Finally通常用于“清理”操作,如处理打开的连接,但您似乎将Finally用于新的选择和填充操作,这是错误的
因此:
或者,您是否需要知道某个特定块何时失败,但每个块都在try/catch中
try
{
[block1]
}
...
try
{
[block4]
}
你为什么要建立这样的联系 你可以这样做
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
}
这很容易——去掉所有的try/catch块。您不明白异常处理是关于什么的
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
}