[英]how to reduce in multiple try catch
嗨,我是開發項目的初學者,在我的項目中,我在try catch中使用了try catch with,所以我如何專業地編寫查詢...
我的代碼是...
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
用於新的select&populate操作,這是錯誤的。
所以:
try
{
[block1]
[block2]
[block3]
[block4]
}
或者您是否需要知道某個特定的塊何時失敗,但是每個都在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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.