程序发布到IIs后,运行不到15分钟就报连接池满的错误,其实数据库连接用完后都关闭了,一朋友说是查询语句效率太低,或查询时用了循环或嵌套循环,让我有话sql语句,我的语句里也没有循环啊.每个关闭后我还加了Dispose();但还是说:连接池满,也试用了maxpoolsiz=512都不能解决问题。.net的高手们,怎么办呢?????????
解决方案 »
- 探讨 C# 对 XML 的操作,两节点运算(如相乘)得到新节点
- response.Redirect 传递多个值!急!!!在线等
- 请问DataGrid分页的时候 删除信息为什么要点2次才能删除?
- 安装VS.net2003里出了点问题,帮忙解决一下
- foreach问题,高手看看
- The request failed with HTTP status 403: Forbidden
- 用varchar还是nvarchar ?
- 双引号的转义符是\"这个吗?
- 窗体认证,如何设置webconfig使得,web上的控件只读变灰?
- 为什么我新建一个web service的时候会出错呢?很烦阿!麻烦帮一帮手!
- 写了语段无聊的程序!!!
- 求教一个常识性问题
使用
using(数据对像)
{
}
他的问题是在程序中大量的用循环new一个类,有的还是嵌套new一个类,
这样内存很快被用光,后来修改了程序,避免大量的用循环new一个类,就OK了!!
楼主情况不知怎样??
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Customertotal : System.Web.UI.Page
{
protected int i;
protected string method = "";//为是否显示公司名称而定义
protected void Page_Load(object sender, EventArgs e)
{
method = Request.QueryString["method"];
if (!IsPostBack)
{//页面第一次加载时,显示所有客户联系记录
//DropDownListjsales
//DataTable saledt = Manager.empdata();
//for (int i = 0; i < saledt.Rows.Count; i++) {
// DropDownListjsales.DataSource = saledt;
// DropDownListjsales.DataTextField = "EmpName";
// DropDownListjsales.DataValueField = "EmpId";
// DropDownListjsales.DataBind();
//}
if (method == "" || method == null)
{
Session["method"] = "kong";
this.PlaceHolderlist.Visible = true;
this.PlaceHolderinfo.Visible = false;
DataTable dt = DataBase.getds("select l_id,c_id,c_name,EmpName,c_comp,l_time,l_content,l_fl from v_test order by l_id asc").Tables[0];
AspNetPager1.RecordCount = dt.Rows.Count;
databind();
}
if (method == "find")
{ Session["method"] = "find";
this.PlaceHolderlist.Visible = true;
this.PlaceHolderinfo.Visible = false;
string cid = Request.QueryString["cid"];
if (cid != "")
{//编号为cid的客户联系记录列表,
Session["lcid"] = cid;
DataTable dt2 = Manager.contractinfo(cid);
if (dt2.Rows.Count >= 1)
{
Session["lcom"] = dt2.Rows[0]["c_comp"].ToString();//为从customer_info.aspx转来的页显示公司名称
this.txtjcomp.Text = dt2.Rows[0]["c_comp"].ToString();
this.txtjcomp.ReadOnly = true;
this.txtjcomp.BackColor = System.Drawing.Color.FromArgb(0, 192, 192);
}
else {
Session["lcom"] = "没有联系记录";
}
AspNetPager2.RecordCount = dt2.Rows.Count;
databind2();
}
}
if (method == "info")
{ //编号为cid的客户详细联系信息
this.PlaceHolderlist.Visible = false;
this.PlaceHolderinfo.Visible = true;
string cid = Request.QueryString["cid"];
int lid = 0;
if (Request.QueryString["lid"] != "" && Request.QueryString["lid"] != null)
{
lid = int.Parse(Request.QueryString["lid"]);
}
Session["lcid"] = cid;
if (cid != "")
{
DataTable dt = Manager.contractinfo(lid);
this.txtnum.Text = dt.Rows[0]["c_id"].ToString();
this.txtcomp.Text = dt.Rows[0]["c_comp"].ToString();
this.txtcname.Text = dt.Rows[0]["c_name"].ToString();
this.txttime.Text = dt.Rows[0]["l_time"].ToString();
this.txtsort.Text = dt.Rows[0]["l_sort"].ToString();
if (Session["username"] != null)
{
this.txtuname.Text = Session["username"].ToString();
}
this.txtcontent.Text = dt.Rows[0]["l_content"].ToString();
this.txtinfosort.Text = dt.Rows[0]["l_fl"].ToString();
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
Response.Redirect("Contract.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
Response.Redirect("ContractRecond.aspx?method=find&cid="+txtnum.Text);//从新增页面返回联系记录列表 }
protected void Button4_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
Response.Redirect("Contract.aspx"); }
protected void Button3_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
if (Session["lcid"] != null)
{
Response.Redirect("Customer_info.aspx?cid=" + Session["lcid"].ToString());
} }
//联系记录查询
protected void selectrecond_Click(object sender, EventArgs e)
{
Session["jsj"] = Request.Form["jsj"];//记录开始时间
if (Request.Form["jej"] == "")//记录结束时间
{
Session["jej"] = DateTime.Now.Date.ToString();
}
else
{
Session["jej"] = Request.Form["jej"] + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;
}
Session["jcomp"] = txtjcomp.Text;
Session["jcname"] = txtjcname.Text;
Session["juname"] = DropDownListjsales.SelectedItem.Text;
if (DropDownListjsales.SelectedValue == "不限")
{
Session["juname"] = "";
}
string[] dl=new string[2];
for (int i = 0; i < CheckBoxListfl.Items.Count; i++) {
if (CheckBoxListfl.Items[i].Selected)
{
dl[i] = CheckBoxListfl.Items[i].Value;
}
}
Session["dl"]=dl;
DataTable dt=null;
if(dl[0]!=null && dl[1]==null){
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), dl[0], "");
}
if (dl[1] != null && dl[0]==null) {
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), "", dl[1]);
}
if (dl[1] != null && dl[0] != null) {
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), dl[0], dl[1]);
}
if (dl[0] == null && dl[1] == null) {
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), "", "");
}
AspNetPager3.RecordCount = dt.Rows.Count;
databind3();
}
{
databind(); }
protected void AspNetPager2_PageChanged(object sender, EventArgs e)
{
databind2(); }
protected void AspNetPager3_PageChanged(object sender, EventArgs e)
{
databind3(); }
private void databind() {
this.AspNetPager1.Visible = true;
this.AspNetPager2.Visible = false;
this.AspNetPager3.Visible = false;
DataTable dt = DataBase.getds("select l_id,c_id,c_name,EmpName,c_comp,l_time,l_content,l_fl from v_test order by l_id asc", AspNetPager1.CurrentPageIndex).Tables[0];
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
AspNetPager1.CustomInfoHTML = "记录总数:<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>";
AspNetPager1.CustomInfoHTML += " 总页数:<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
private void databind2()
{
this.AspNetPager3.Visible = false;
this.AspNetPager1.Visible = false;
this.AspNetPager2.Visible = true;
DataTable dt = DataBase.getds("select l_id,c_id,c_name,EmpName,c_comp,l_time,l_content,l_fl from v_test where c_id='" + Session["lcid"].ToString() + "'", AspNetPager2.CurrentPageIndex).Tables[0];
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
AspNetPager2.CustomInfoHTML = "记录总数:<font color=\"blue\"><b>" + AspNetPager2.RecordCount.ToString() + "</b></font>";
AspNetPager2.CustomInfoHTML += " 总页数:<font color=\"blue\"><b>" + AspNetPager2.PageCount.ToString() + "</b></font>";
AspNetPager2.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager2.CurrentPageIndex.ToString() + "</b></font>";
}
private void databind3()
{
this.AspNetPager2.Visible = false;
this.AspNetPager1.Visible = false;
this.AspNetPager3.Visible = true;
DataTable dt=null;
string[] dl=(string[])Session["dl"];
if (dl[0] != null)
{
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(), Session["jcomp"].ToString(), Session["jcname"].ToString(), Session["juname"].ToString(), dl[0], "", AspNetPager3.CurrentPageIndex);
}
if (dl[1] != null)
{
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), "", dl[1], AspNetPager3.CurrentPageIndex);
}
if (dl[1] != null && dl[0] != null)
{
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), dl[0], dl[1], AspNetPager3.CurrentPageIndex);
}
if (dl[0] == null && dl[1] == null)
{
dt = Manager.Select_lf(Session["jsj"].ToString(), Session["jej"].ToString(),Session["jcomp"].ToString(),Session["jcname"].ToString(), Session["juname"].ToString(), "", "", AspNetPager3.CurrentPageIndex);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
AspNetPager3.CustomInfoHTML = "记录总数:<font color=\"blue\"><b>" + AspNetPager3.RecordCount.ToString() + "</b></font>";
AspNetPager3.CustomInfoHTML += " 总页数:<font color=\"blue\"><b>" + AspNetPager3.PageCount.ToString() + "</b></font>";
AspNetPager3.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager3.CurrentPageIndex.ToString() + "</b></font>";
}
}
下面是一个类的代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;/// <summary>
/// Manager 的摘要说明
/// </summary>
public class Manager
{
public Manager()
{ }
//立项
public static int addlx(string xproc, string xname, DateTime xstime, DateTime xetime, string names, string xre)
{
SqlParameter[] parame ={
new SqlParameter("@proc",SqlDbType.VarChar,50),
new SqlParameter("@xname",SqlDbType.VarChar,50),
new SqlParameter("@stime",SqlDbType.DateTime),
new SqlParameter("@etime",SqlDbType.DateTime),
new SqlParameter("@uname",SqlDbType.VarChar,50),
new SqlParameter("@re",SqlDbType.VarChar,50),
new SqlParameter("@xid",SqlDbType.VarChar)
};
parame[0].Value = xproc;
parame[1].Value = xname;
parame[2].Value = xstime;
parame[3].Value = xetime;
parame[4].Value = names;
parame[5].Value = xre;
parame[6].Value = projectnum();
string strsql = "insert into lixi(x_id,x_proc,x_name,x_stime,x_etime,u_name,x_re)values(@xid,@proc,@xname,@stime,@etime,@uname,@re)";
int n = DataBase.getExecuteNonQuery(strsql, parame);
return n; }
//生成项目编号
public static string projectnum()
{
string str = "XM" + DateTime.Now.ToString("yyyyMM");
string xid = "";
int xids = 0;
string sqlnum = "select top 1 x_id from lixi order by x_ltime desc";
DataSet ds = DataBase.getds(sqlnum);
if (ds.Tables[0].Rows.Count == 0)
{
xid = str + "000";
}
else
{
xid = ds.Tables[0].Rows[0]["x_id"].ToString();
xids = int.Parse(xid.Substring(8, 4)) + 1;
if (xids < 10)
{
xid = str + "00" + xids.ToString();
}
if (xids >= 10 && xids < 100)
{
xid = str + "0" + xids.ToString();
}
if (xids >= 100 && xids < 1000)
{
xid = str + xids.ToString();
}
}
return xid;
}
// 添加客户信息
public static int addcustomer(string cid,string cname,string sort,string sex,string position,string qq,string mobile,string comp,string addr,string code,string tel,string rex,string email,string uname,string hy,string re,string website,string info,string jl) {
SqlParameter[] pararme ={
new SqlParameter("@cid",SqlDbType.VarChar),
new SqlParameter("@cname",SqlDbType.VarChar),
new SqlParameter("@sort",SqlDbType.Int),
new SqlParameter("@sex",SqlDbType.VarChar),
new SqlParameter("@position",SqlDbType.VarChar),
new SqlParameter("@qq",SqlDbType.VarChar),
new SqlParameter("@mobile",SqlDbType.VarChar),
new SqlParameter("@comp",SqlDbType.VarChar),
new SqlParameter("@addr",SqlDbType.VarChar),
new SqlParameter("@code",SqlDbType.VarChar),
new SqlParameter("@tel",SqlDbType.VarChar),
new SqlParameter("@rex",SqlDbType.VarChar),
new SqlParameter("@email",SqlDbType.VarChar),
new SqlParameter("@uname",SqlDbType.Int),
new SqlParameter("@hy",SqlDbType.Int),
new SqlParameter("@re",SqlDbType.VarChar),
new SqlParameter("@website",SqlDbType.VarChar),
new SqlParameter("@info",SqlDbType.VarChar),
new SqlParameter("@jl",SqlDbType.VarChar)
};
pararme[0].Value = cid;
pararme[1].Value = cname;
pararme[2].Value = Convert.ToInt32(sort);
pararme[3].Value = sex;
pararme[4].Value = position;
pararme[5].Value = qq;
pararme[6].Value = mobile;
pararme[7].Value = comp;
pararme[8].Value = addr;
pararme[9].Value = code;
pararme[10].Value = tel;
pararme[11].Value = rex;
pararme[12].Value = email;
pararme[13].Value = Convert.ToInt32(uname);
pararme[14].Value = Convert.ToInt32(hy);
pararme[15].Value = re;
pararme[16].Value = website;
pararme[17].Value = info;
pararme[18].Value = jl;
string strsql = "insert into customer(c_id,c_name,v_id,c_sex,c_position,c_qq,c_mobile,c_comp,c_addr,c_code,c_tel,c_rex,c_email,EmpId,h_id,c_re,c_website,c_info,c_jl)" +
"values(@cid,@cname,@sort,@sex,@position,@qq,@mobile,@comp,@addr,@code,@tel,@rex,@email,@uname,@hy,@re,@website,@info,@jl)";
int n = DataBase.getExecuteNonQuery(strsql, pararme);
return n; }
//添加联系记录cid, comp, cname, sort, ltime, content, uname
public static int addcontrat(string cid, string comp, string cname, string lsort, DateTime ltime, string content, string uname,string fl)
{
SqlParameter[] paramer ={
new SqlParameter("@cid",SqlDbType.VarChar),
new SqlParameter("@comp",SqlDbType.VarChar),
new SqlParameter("@cname",SqlDbType.VarChar),
new SqlParameter("@lsort",SqlDbType.VarChar),
new SqlParameter("@ltime",SqlDbType.DateTime),
new SqlParameter("@content",SqlDbType.VarChar),
new SqlParameter("@uname",SqlDbType.Int),
new SqlParameter("@fl",SqlDbType.VarChar)
};
paramer[0].Value = cid;
paramer[1].Value = comp;
paramer[2].Value = cname;
paramer[3].Value = lsort;
paramer[4].Value = ltime;
paramer[5].Value = content;
paramer[6].Value = Convert.ToInt32(uname);
paramer[7].Value = fl;
string strsql = "insert into contract(c_id,l_comp,c_name,l_sort,l_time,l_content,EmpId,l_fl)values(@cid,@comp,@cname,@lsort,@ltime,@content,@uname,@fl)";
int n = DataBase.getExecuteNonQuery(strsql, paramer);
return n; }
using()
{
}代码怎么感觉有点乱~
同楼上的,session能少用尽量少用~
也不用一个session存一个值吧~
一般能不用session的都不要用session。
Session["lcid"] = cid; protected void Button3_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
if (Session["lcid"] != null)
{
Response.Redirect("Customer_info.aspx?cid=" + Session["lcid"].ToString());
} } ====================================================
随便拿一个代码,像上面这些代码就可以不用Session了,可以改为:
protected void Button3_Click(object sender, EventArgs e)
{//新增联系信息,cid在session看存放
if(!string.IsNullOrEmpty(Request.QueryString["cid"]))
{
Response.Redirect("Customer_info.aspx?cid=" + Request.QueryString["cid"]);
} }
传进来的参数最好统一写在Page_Load中方便点..
你看看相关配置是否有访问时间限制,容量限制等
切换配置试一下