两个SqlDataReader一起用
SqlCommand commf=new SqlCommand( "select * from hg where hgbh= ' "+gh+ " ' ",Conn);
SqlDataReader sdrf=commf.ExecuteReader();
while(sdrf.Read())
{
SqlCommand commff=new SqlCommand( "select * from ckmx1 where ddbh= ' "+sdrf[ "ddbh "].ToString()+ " ' ",Conn);
SqlDataReader sdrff=commff.ExecuteReader();
while(sdrff.Read())
{
ddbh=sdrff[ "ddbh "].ToString();ys=sdrff[ "ys "].ToString();
girdbin();
girdbin1();
girdbin2();
}
sdrff.Close();
}
sdrf.Close();
Conn.Close();
我想做到这样的功能效果..girdbin();girdbin1();girdbin2();里面还有同样的while循环..请问我该怎么办?
------解决方案--------------------由于DataReader对连接是独占的,所以你每个dataReader都用同一个Connection肯定是有问题..
如果你想层层嵌套来用DataReader的话,那就有多少个DataReader,就建立几个SqlConnection..
SqlConnection con1=new SqlConnection( ".. ");
SqlConnection con2=new SqlConnection( ".. ");
SqlCommand cmd1=new SqlCommand( ".. ",con1);
SqlDataReader sdr1=cmd.ExecuteReader();
while(sdr1.Read())
{
SqlCommand cmd2=new SqlCommand( ".. ",con2);
SqlDataReader sdr=cmd2.ExecuteReader();
..
}
类似这种,虽然效率可能不高,但还是可以用的..