SqlConnection myConnection1 = new SqlConnection(connString); SqlConnection myConnection2 = new SqlConnection(connString); SqlConnection myConnection3 = new SqlConnection(connString); 现在,我们有了使用池中的若干连接的代码。首先,打开两个连接并返回这两个到池的连接。然后,从池中打开三个连接,并返回所有这三个到池的连接。
// Open two connections. One is from the pool (see min pool size), the other is created. Console.WriteLine ("Open two connections."); myConnection1.Open(); myConnection2.Open();
// Now there are two connections in the pool that matches the connection string. // Return the both connections to the pool. Console.WriteLine ("Return both of the connections to the pool."); myConnection1.Close(); myConnection2.Close();
// Get a connection out of the pool. Console.WriteLine ("Open a connection from the pool."); myConnection1.Open();
// Get a second connection out of the pool. Console.WriteLine ("Open a second connection from the pool."); myConnection2.Open();
// Open a third connection. Console.WriteLine ("Open a third connection."); myConnection3.Open();
// Return the all connections to the pool. Console.WriteLine ("Return all three connections to the pool."); myConnection1.Close(); myConnection2.Close(); myConnection3.Close(); 池连接的模型与非池连接的相似。但是,当断开池连接来将其释放回池时,调用 Close 是特别重要的。
</ul> </ItemTemplate> </asp:Repeater> 后台:page-load中 ds = OLEDBcon.Dataset("select * from departments order by departmentid ", con); Repeater1.DataSource = ds; Repeater1.DataBind();protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rep = (Repeater)e.Item.FindControl("Repeater2"); System.Data.DataRowView view = (System.Data.DataRowView)e.Item.DataItem; int departmentid= Convert.ToInt32(view["departmentid"].ToString()); OleDbConnection con = OLEDBcon.Con(); con.Open(); DataSet ds = OLEDBcon.Dataset("select * from taskuser where departmentid=" + departmentid+ " order by usercardid",con); rep.DataSource = ds; rep.DataBind(); con.Close(); } }上面是我百度出来的,他也是这样做!!!频繁开关数据库!!
没什么问题,因为有连接池,前提是你要close他,他才会返回池中,
你错了,我刚刚做个这个实验!明显是先执行数据库,后进入下一个循环:protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.\\SqlExpress;database=GuestBook;Integrated Security=True"); for (int i = 0; i < 5;i++ ) { Response.Write("The Page Data<br/>"); SqlDataAdapter sda = new SqlDataAdapter("Select UserName from Users", conn); DataSet ds = new DataSet(); sda.Fill(ds); Response.Write("The Sql Data:" + ds.Tables[0].Rows[0][0].ToString()+"<br/>"); } }显示结果: The Page Data The Sql Data:SQLDATA The Page Data The Sql Data:SQLDATA The Page Data The Sql Data:SQLDATA The Page Data The Sql Data:SQLDATA The Page Data The Sql Data:SQLDATA
<ItemTemplate>
<div><%#ShowName(Eval("id").ToString()) %></div>
</ItemTemplate>
</asp:Repeater>public string ShowName(string strID)
{
database.open();
.....获取name
database.close();
return name;
}
用联合查询是会破坏我的三层架构的。
【还有不是某个页面卡住,是整站卡住,没用到那种形式的页面也卡住】
<ItemTemplate>
<h1>Eval("CityName").ToString())<h1>
<div>
<asp:Repeater ID="repCountry" runat="server">
<ItemTemplate>
<div><%#ShowCountry(Eval("Cityid").ToString()) %></div>
<asp:Repeater ID="repCity" runat="server">
<ItemTemplate>
</div>
</ItemTemplate>
</asp:Repeater>public string ShowCountry(string strCountryID)
{
conn.open();
....根据cityid获取县城名字
conn.close();
return name;
}
就像ms petshop假如做个那么大的系统写成petshop那样那肯定是疯子 但petshop只是一个例子而已
database.open();
database.close();
唉
<ItemTemplate>
<h1>Eval("CityName").ToString()) <h1>
<div>
<asp:Repeater ID="repCountry" runat="server">
<ItemTemplate>
<div> <%#ShowCountry(Eval("Cityid").ToString()) %> </div>
<asp:Repeater ID="repCity" runat="server">
<ItemTemplate>
</div>
</ItemTemplate>
</asp:Repeater>
public string ShowCountry(string strCountryID)
{
conn.open();
....根据cityid获取县城名字
conn.close();
return name;
}
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<div><%#Eval("id")%>(<%#ShowCount(Eval("id").ToString()%>))条</div>
</ItemTemplate>
</asp:Repeater>conn.open();
rep.DataSource= "select top 10 id from t1"
rep.Bind();
conn.closepublic string ShowCount(string strid)
{
conn.open();
string num=select count(1) from t2 where id= strid;
conn.close();
return num;
}
Repeater 绑定 1万条数据还是能承受的了得
可以加上缓存试试
数据库连接的资源是非常宝贵的。数据量不是非常的多的时候,尽量一次查询出来。如果数据量非常多呢,就分批处理了。对于数据库的操作一般的方案是最晚open,然后最快colse。
这句话倒是对的。我就是出现了这种情况。 我就是问我频繁打开了,但是我有关闭啊,没个for都有关闭,为什么线程池还会爆呢?
Repeater 绑定 1万条数据还是能承受的了得
可以加上缓存试试不是数据多,而是频繁打开关闭数据库连接。不是数据多的问题。不要跑题了。
的确占了很多,但是我就奇怪,我都有执行 Close dispose,干嘛就不会断呢?
循环执行打开关闭 如果有操作的数据库的行为,即不会关闭,也可能会发生死锁,因为你上一个操作还没有完成 又是一个新的操作循环的时候,应该是数据库执行完毕之后才会进入下一个循环吧! 代码是从上往下执行的!而且我都加了 with(nolock)
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<div><%#Eval("id")%>(<%#ShowCount(Eval("id").ToString()%>)条回复)</div>
</ItemTemplate>
</asp:Repeater>
conn.open();
rep.DataSource= "select top 10 id from t1"
rep.Bind();
conn.close
public string ShowCount(string strid)
{
conn.open();
string num= "select count(1) from t2 where id= strid";
conn.close();
return num;
}
========================
为什么不对呢?(3条回复)
干嘛你也不会做呢?(7条回复)
你还是先帮我讲讲不用那样做,应该怎么做?(3条回复)
你写出来啊,不用for,用怎样做,取出每个问题,并写回复数,不准改我数据库表?(0条回复)
====================================================
那你帮我实现这种方式,你说怎么写
<ItemTemplate>
<div><%#Eval("Title")%>(<%#ShowCount(Eval("id").ToString()%>)条回复)</div>
</ItemTemplate>
</asp:Repeater>conn.open();
rep.DataSource= "select top 10 id from t1"
rep.Bind();
conn.closepublic string ShowCount(string strid)
{
conn.open();
string num= "select count(1) from t2 where id= strid";
conn.close();
return num;
}
==================================
为什么不对呢?(3条回复)
干嘛你也不会做呢?(7条回复)
你还是先帮我讲讲不用那样做,应该怎么做?(3条回复)
你写出来啊,不用for,用怎样做,取出每个问题,并写回复数,不准改我数据库表?(0条回复)
请考虑一下这个解决方案哦?(0条回复)
====================================
这样哦。。虽然破坏了我的三层架构,但是如果这样执行速度更快的话,我还是用这样吧。
对于repeater 嵌套,我还是考虑用 relation吧只能这样了,对吧?
好滴。不过有时OO,有时不OO,整个网站看起来很别扭那我就都想办法改改吧。网站太经常卡住了。而且卡住原因,就是应该连接数太多。因为报错是 连接超时,什么连接池什么的。。
还要使用Using实现资源的释放
还要使用Using实现资源的释放我就是模仿他的SQLHELPER
在此示例中,在构造 SqlConnection 时在连接字符串中指定池特性,如以下的代码示例中所示。请注意:池处理是隐式的,除非将其禁用,否则将自动进行池处理。因此,"true"是默认的池关键词 (pooling=true)。
String connString;
connString = "server=(local)\\VSdotNET;Trusted_Connection=yes;database=northwind;" +
"connection reset=false;" +
"connection lifetime=5;" +
"min pool size=1;" +
"max pool size=50";
SqlConnection myConnection1 = new SqlConnection(connString);
SqlConnection myConnection2 = new SqlConnection(connString);
SqlConnection myConnection3 = new SqlConnection(connString);
现在,我们有了使用池中的若干连接的代码。首先,打开两个连接并返回这两个到池的连接。然后,从池中打开三个连接,并返回所有这三个到池的连接。
// Open two connections. One is from the pool (see min pool size), the other is created.
Console.WriteLine ("Open two connections.");
myConnection1.Open();
myConnection2.Open();
// Now there are two connections in the pool that matches the connection string.
// Return the both connections to the pool.
Console.WriteLine ("Return both of the connections to the pool.");
myConnection1.Close();
myConnection2.Close();
// Get a connection out of the pool.
Console.WriteLine ("Open a connection from the pool.");
myConnection1.Open();
// Get a second connection out of the pool.
Console.WriteLine ("Open a second connection from the pool.");
myConnection2.Open();
// Open a third connection.
Console.WriteLine ("Open a third connection.");
myConnection3.Open();
// Return the all connections to the pool.
Console.WriteLine ("Return all three connections to the pool.");
myConnection1.Close();
myConnection2.Close();
myConnection3.Close();
池连接的模型与非池连接的相似。但是,当断开池连接来将其释放回池时,调用 Close 是特别重要的。
一次取出什么? 一次取出所有子项? 那我怎么嵌套呢?怎么绑定呢? 问题为外repeater,回复为嵌套的repeater。 问题表和回复表数据很多,现在我取20条问题,20条问题下面的回复都取出。像这种效果:
10个月大的宝宝戒奶以后可以喝核桃奶或者纯牛奶吗
是的
不是的
可能吧
ford shelby 和 ford mustang的分别在哪里?
是的
不是的
点击天涯社区时路由器掉网问题?
是的
不是的
oppoT8能下载什么样的手机QQ软件?
是的
不是的
男1982.6.28晚八点 ?
是的
不是的
程序中的循环快?还是打开关闭数据连接速度快?毋庸置疑肯定是循环快了既然循环快,那你每次打开数据库连接,但是还没来得及关闭会产生什么后果?卡死
循环的时候,应该是数据库执行完毕之后才会进入下一个循环吧! 代码是从上往下执行的!而且我都加了 with(nolock)学过多线程没?这个所用于线程同步,不是你在循环里加了后,就停止循环了
给个完整的解决方案先我百度了一下,大家都是我原来那样做的啊。都是在ItemDataBound findcontrol repeater 然后再去绑定啊。
10个月大的宝宝戒奶以后可以喝核桃奶或者纯牛奶吗
是的
不是的
可能吧
ford shelby 和 ford mustang的分别在哪里?
是的
不是的
点击天涯社区时路由器掉网问题?
是的
不是的
oppoT8能下载什么样的手机QQ软件?
是的
不是的
男1982.6.28晚八点 ?
是的
不是的
onitemdatabound="Repeater1_ItemDataBound" >
<ItemTemplate >
<ul>
<li> <input type="checkbox" value='<%#Eval("departmentid") %>' id="checkbox1" runat ="server" />
<span><%#Eval("departmentsname")%></span>
<asp:Repeater ID="Repeater2" runat="server" >
<ItemTemplate >
<ul>
<li>
<input type="checkbox" value='<%#Eval("usercardid") %>' id ="checkbox2" runat ="server" /><span><%#Eval("username")%></span>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
</li>
</ul>
</ItemTemplate>
</asp:Repeater> 后台:page-load中 ds = OLEDBcon.Dataset("select * from departments order by departmentid ", con);
Repeater1.DataSource = ds;
Repeater1.DataBind();protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = (Repeater)e.Item.FindControl("Repeater2");
System.Data.DataRowView view = (System.Data.DataRowView)e.Item.DataItem;
int departmentid= Convert.ToInt32(view["departmentid"].ToString());
OleDbConnection con = OLEDBcon.Con();
con.Open();
DataSet ds = OLEDBcon.Dataset("select * from taskuser where departmentid=" + departmentid+ " order by usercardid",con);
rep.DataSource = ds;
rep.DataBind();
con.Close();
}
}上面是我百度出来的,他也是这样做!!!频繁开关数据库!!
你错了,我刚刚做个这个实验!明显是先执行数据库,后进入下一个循环:protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.\\SqlExpress;database=GuestBook;Integrated Security=True");
for (int i = 0; i < 5;i++ )
{
Response.Write("The Page Data<br/>");
SqlDataAdapter sda = new SqlDataAdapter("Select UserName from Users", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
Response.Write("The Sql Data:" + ds.Tables[0].Rows[0][0].ToString()+"<br/>"); } }显示结果:
The Page Data
The Sql Data:SQLDATA
The Page Data
The Sql Data:SQLDATA
The Page Data
The Sql Data:SQLDATA
The Page Data
The Sql Data:SQLDATA
The Page Data
The Sql Data:SQLDATA
{
}
http://topic.csdn.net/u/20090912/12/7f191a81-ef8a-40c5-a08a-ac30f71fb420.html
[QUOTE]可能你不知道,SqlConnection是有连接池的,也就是说Open/Close它并不一定是打开、关闭物理连接。例如你在For循环里循环10000次来创建、打开、访问、关闭数据库,可能也只是使用了2、3个数据库连接而不是10000个。 如何重复使用数据库连接是.net自己早已经处理好的事情,你就应该老老实实地Open/Close就可以了。自己去写“共享”连接反而是画蛇添足。[QUOTE]按他这种说法,那样写根本没问题的! !!