日期:2014-05-18  浏览次数:20469 次

请大伙帮忙看一下sdr.Read();这个的问题!不够+++分!帮忙的哥们来!
问题出现在if(   sdr.Read()   )这个地方!这个我判断了如果能读读取出来!
但是它和下面的while(   sdr.Read()   )发生冲突程序不出现错误,但是while的地方“指向”数据库第二条数据,我怎么才能让他指在第一条数据上呢??
程序源码如下:
————————————————
SqlConnection   con   =   showclass.datacon.AutoCon();
con.Open();

SqlCommand   cmd   =   new   SqlCommand( "SelectDealer ",con);
cmd.CommandType   =   CommandType.StoredProcedure;

SqlParameter   uid   =   new   SqlParameter( "@pid ",SqlDbType.Int,4);
uid.Value   =   Convert.ToInt32(   pid   );
cmd.Parameters.Add(   uid   );

SqlDataReader   sdr   =   cmd.ExecuteReader();

System.Text.StringBuilder   Jxs   =   new   System.Text.StringBuilder();

if(   sdr.Read()   )
{
Jxs.Append( " <table   cellpadding=\ "0\ "   cellspacing=\ "0\ "   class=\ "pplj-gd\ "> ");
Jxs.Append( " <tr> ");
Jxs.Append( " <td   class=\ "zi\ "> 首   页   &gt;   "+sdr[ "autobrand "].ToString()+ " "+sdr[ "autotype "].ToString()+ "   经销商列表   &gt;   "+sdr[ "autobrand "].ToString()+ " "+sdr[ "autotype "].ToString()+ " ");
Jxs.Append( " </td> ");
Jxs.Append( " </tr> ");
Jxs.Append( " </table> ");
Jxs.Append( " <div   id=\ "pplj-1\ "> ");
Jxs.Append( " <div   class=\ "ppljzuo\ "> [ "+sdr[ "autobrand "].ToString()+ " "+sdr[ "autotype "].ToString()+ "]经销商 </div> ");
Jxs.Append( " <div   class=\ "ppljyou\ "> (共 <span   class=\ "zi\ "> 17 </span> 家)&nbsp; </div> ");
Jxs.Append( " </div> ");


while(   sdr.Read()   )
{
Jxs.Append( " <table   cellpadding=\ "0\ "   cellspacing=\ "0\ "   class=\ "pplj-5\ "> ");
Jxs.Append( " <tr> ");
Jxs.Append( " <td   class=\ "pplj-6\ "> <span   class=\ "pplj-zi\ "> "+sdr[ "user_dw "].ToString()+ " </span> </td> ");
Jxs.Append( " <td   class=\ "pplj-6-1\ "> "+sdr[ "user_dz "].ToString()+ " </td> ");
Jxs.Append( " <td   class=\ "pplj-6-2\ "> <a   href=\ " "+sdr[ "user_main "].ToString()+ "\ "> 详细&gt;&gt; </a> </td> ");
Jxs.Append( " </tr> ");
Jxs.Append( " </table> ");
}
}

cmd.Dispose();
sdr.Close();
con.Close();
con.Dispose();
return   Jxs.ToString();

------解决方案--------------------
应该直接while( sdr.Read() ),因为sdr是只向前的,不能后退的!!!
------解决方案--------------------
bool flag = true;

while( sdr.Read() )
{
if(flag)
{
Jxs.Append( " <table cellpadding=\ "0\ " cellspacing=\ "0\ " class=\ "pplj-gd\ "> ");
Jxs.Append( " <tr> ");
Jxs.Append( " <td class=\ "zi\ "> 首 页 &gt; "+sdr[ "autobrand "].ToString()+ " "+sdr[ "autotype "].ToString()+ " 经销商列表 &gt;