首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求gridview绑定有关问题,解决就给分

2012-01-20 
求gridview绑定问题,急急,解决就给分我有两个表表AaudioIDaudioNameUserIdaudioRemark表BIDCommTxt(留言)a

求gridview绑定问题,急急,解决就给分
我有两个表
表A
audioID         audioName   UserId   audioRemark

表B

ID     CommTxt(留言)   audioID


我想用gridview     通过UserId显示某个用户所有的audio信息

这样显示

audioName     audioRemark     countcomm(统计留的次数)


这个怎么做,用不用gridview   嵌套  

看看,我下面做的,,这个有问题,,谢谢指点!!!


public   int   commcount;     在html里绑定这个统计字段,

  private   void   MyCreateBind()
        {

                int   uid   =   Int32.Parse(Session[ "UserId "].ToString());

                string   str   =   "select   *   from   UsersAudioView   where   UserId= "   +   uid;

                SqlConnection   myCon   =   new   SqlConnection(ConfigurationManager.ConnectionStrings[ "mysql "].ConnectionString);
                myCon.Open();

                SqlDataAdapter   sda   =   new   SqlDataAdapter(str,   myCon);
                DataSet   ds   =   new   DataSet();
                sda.Fill(ds,   "UsersAudioView   ");
                AudioInfoGV.DataSource   =   ds.Tables[ "UsersAudioView   "];
                AudioInfoGV.DataBind();
                myCon.Close();
        }


      protected   void   audioInfoGV_RowDataBound(object   sender,   GridViewRowEventArgs   e)
        {


    if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                {


                       
                      //   DataList   dali   =   (DataList)e.Row.FindControl( "DataList1 ");
                       
                        Label   lblvid   =   (Label)e.Row.FindControl( "lblaudioId ");
     


                        string   aid=   lblvid.Text.ToString();

                       
                        string   str   =   "select   count(*)as   aa   from   表B   where   audioId= ' "   +   aid+   " ' ";


                        SqlConnection   myCon   =   new   SqlConnection(ConfigurationManager.ConnectionStrings[ "mysql "].ConnectionString);
                        myCon.Open();



                        SqlCommand   myCmd   =   new   SqlCommand(str,   myCon);

                      //   SqlDataAdapter   sda   =   new   SqlDataAdapter(str,   myCon);
                    //     DataSet   ds   =   new   DataSet();
                      //   sda.Fill(ds);
                        SqlDataReader   dr   =   myCmd.ExecuteReader();


                        if   (dr.Read())
                        {

                                commcount   =   Int32.Parse(dr[ "aa "].ToString());


                        }
                        else
                        {

                                commcount   =   0;


                        }
                      //   dr.Close();
                      //   dali.DataSource   =   ds;
                      //   dali.DataBind();
                      dr.close();

                }
}

[解决办法]
給我的感覺,你的程序在audioInfoGV_RowDataBound里面將會運行的很慢,我覺得你不需要去按每行都是查詢B表一次,這樣速度太慢了。建議:國為我不知道你要顯示的字段,所以分步處理。
DataSet ds = new DataSet();
string strA = "select * from A where UserId= " + uid;
.......
sda.Fill(ds , "A ");

string strB = "select A.audioId,count(B.*) as aa from A inner join B on A.audioId = b.audioId where A.UserId = " + uid + " Group by A.audioId "
sda.fill(ds , "B ");

這樣,A表與B表就是一對一的關關系,A表的留言條數就在B表的aa字段里面,並連字段為audioId。(如果A表的某個audioId在B表中沒有記錄關連,那麼它的留言記錄就是0.)

热点排行