我的.cs
文件中有以下代码。这是代码示例。还有更多if..elseif...else if..
等条件。为了减少代码,我只附加了只有2个条件的示例代码if和else ..
当我在IIS 7.0上运行此模块时,会出现与连接池超时相关的错误。
我的问题是如何根据我的代码情况完全关闭连接?
public class GenerateSubjectWiseReportDAL
{
string ConnectionString = "";
string query = "";
SqlCommand com;
SqlConnection con;
public GenerateSubjectWiseReportDAL()
{
ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
}
try
{
// bexcel.Visible = true;
con = new SqlConnection(ConnectionString);
con.Open();
if (sessional.Equals("1"))
{
if (lbsubjecttype.Equals("L"))
{
query = "SELECT DISTINCT Attendence_Stu_MainL1.st_id, Attendence_Stu_MainL1.st_rollno, Attendence_Stu_MainL1.st_name, Attendence_Stu_MainL1.scode, Attendence_Stu_MainL1.attendence AS Expr6, " +
" Attendence_Stu_MainL1.total AS Expr7 FROM Attendence_Stu_MainL1 WHERE ((Attendence_Stu_MainL1.sem_no=@semno AND Attendence_Stu_MainL1.scode=@scode) AND Attendence_Stu_MainL1.branch_name=@branchname) ORDER BY Attendence_Stu_MainL1.scode,Attendence_Stu_MainL1.st_rollno";
GridView1.Columns[4].Visible = false;
GridView1.Columns[5].Visible = false;
GridView1.Columns[8].Visible = false;
GridView1.Columns[9].Visible = false;
Binding(GridView1, lbsubjecttype);
}
else
{
query = "SELECT DISTINCT Attendence_Stu_Main.st_id, Attendence_Stu_Main.st_rollno, Attendence_Stu_Main.st_name, Attendence_Stu_Main.scode, Attendence_Stu_Main.attendence AS Expr4, " +
" Attendence_Stu_Main.total AS Expr5, Attendence_Stu_MainL1.attendence AS Expr6, Attendence_Stu_MainL1.total AS Expr7 FROM Attendence_Stu_Main LEFT OUTER JOIN " +
" Attendence_Stu_MainL1 ON Attendence_Stu_Main.st_id = Attendence_Stu_MainL1.st_id AND Attendence_Stu_Main.scode = Attendence_Stu_MainL1.scode WHERE ((Attendence_Stu_Main.sem_no=@semno AND Attendence_Stu_Main.scode=@scode) AND Attendence_Stu_Main.branch_name=@branchname) ORDER BY Attendence_Stu_Main.scode,Attendence_Stu_Main.st_rollno";
GridView1.Columns[8].Visible = false;
GridView1.Columns[9].Visible = false;
Binding(GridView1, lbsubjecttype);
}
}
}
}
catch (SqlException ee)
{
lbmsg.Text = ee.Message;
lbmsg.Visible = true;
lbmsg.ForeColor = Color.Green;
throw ee;
}
finally
{
con.Close();
}
}
public void Binding(GridView mygridview, string subjtype)
{
try
{
int n = 0;
com = new SqlCommand(query, con);
//Initialize your connection using sqlConnection object
SqlDataAdapter da = new SqlDataAdapter(com);
com.Parameters.AddWithValue("@semno",semno);
com.Parameters.AddWithValue("@scode", code);
com.Parameters.AddWithValue("@branchname", branch);
//Since it's a SELECT statement, the result may be a collection of tables so collect the result in a dataset like this
DataSet ds = new DataSet();
da.Fill(ds, "ss");
com.ExecuteNonQuery();
mygridview.DataSource = ds.Tables["ss"]; ;
mygridview.DataBind();
foreach (GridViewRow g1 in mygridview.Rows)
{
try
{
id = mygridview.Rows[g1.RowIndex].Cells[0].Text;
rollno = mygridview.Rows[g1.RowIndex].Cells[1].Text;
name = mygridview.Rows[g1.RowIndex].Cells[2].Text;
scode = mygridview.Rows[g1.RowIndex].Cells[3].Text;
if (subjtype.Equals("L"))
{
string sql = "INSERT INTO Attendence_Subject_Wise(st_id,st_rollno,st_name,branch_name,sem_no,scode,sess_no,theory,ttotal,lab,ltotal,tutorial,tutotal)"
+ "VALUES (@Stid, @Strollno,@Stname,@BranchName,@SemNo,@Scode,@Sessno,@theory,@total1,@lab,@total2,@tut,@total3)";
con = new SqlConnection(ConnectionString);
con.Open();
com = new SqlCommand(sql, con);
com.Parameters.Add("@Stid", id);
com.Parameters.Add("@Strollno", rollno);
com.Parameters.Add("@Stname", name);
com.Parameters.Add("@BranchName", branch);
com.Parameters.Add("@SemNo", semno);
com.Parameters.Add("@Scode", scode);
com.Parameters.Add("@Sessno", sess);
com.Parameters.Add("@theory", n);
com.Parameters.Add("@total1", n);
com.Parameters.Add("@lab", mygridview.Rows[g1.RowIndex].Cells[6].Text);
com.Parameters.Add("@total2", mygridview.Rows[g1.RowIndex].Cells[7].Text);
com.Parameters.Add("@tut", n);
com.Parameters.Add("@total3", n);
}
else
{
string sql = "INSERT INTO Attendence_Subject_Wise(st_id,st_rollno,st_name,branch_name,sem_no,scode,sess_no,theory,ttotal,lab,ltotal,tutorial,tutotal)"
+ "VALUES (@Stid, @Strollno,@Stname,@BranchName,@SemNo,@Scode,@Sessno,@theory,@total1,@lab,@total2,@tut,@total3)";
con = new SqlConnection(ConnectionString);
con.Open();
com = new SqlCommand(sql, con);
com.Parameters.Add("@Stid", id);
com.Parameters.Add("@Strollno", rollno);
com.Parameters.Add("@Stname", name);
com.Parameters.Add("@BranchName", branch);
com.Parameters.Add("@SemNo", semno);
com.Parameters.Add("@Scode", scode);
com.Parameters.Add("@Sessno", sess);
com.Parameters.Add("@theory", mygridview.Rows[g1.RowIndex].Cells[4].Text);
com.Parameters.Add("@total1", mygridview.Rows[g1.RowIndex].Cells[5].Text);
com.Parameters.Add("@lab", mygridview.Rows[g1.RowIndex].Cells[6].Text);
com.Parameters.Add("@total2", mygridview.Rows[g1.RowIndex].Cells[7].Text);
com.Parameters.Add("@tut", mygridview.Rows[g1.RowIndex].Cells[8].Text);
com.Parameters.Add("@total3", mygridview.Rows[g1.RowIndex].Cells[9].Text);
}
com.ExecuteNonQuery();
con.Close();
}
catch (SqlException ex)
{
lbmsg.Text = ex.Message;
lbmsg.Visible = true;
lbmsg.ForeColor = Color.Green;
}
}
}
catch (SqlException ex)
{
lbmsg.Text = ex.Message;
lbmsg.Visible = true;
lbmsg.ForeColor = Color.Green;
}
}
}