我有两个想法:
1、托个DATASOURCE控件,在控件里把查询条件写好,然后在CS页取出记录个数;
2、用如下代码:SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings"ythzhConnectionString"].ConnectionString);string Plan_Count = "SELECT COUNT(*) FROM Leader_Plan WHERE (ProjectType =  '" + Session["P_T"] + "') AND (PlanDate LIKE '%" + Session["year"] + "%')";
        
SqlCommand scomm = new SqlCommand(Plan_Count, conn);接着怎么写能取出来?
请指教,哪个方法好呢?

解决方案 »

  1.   


    string Plan_Count = "SELECT COUNT(1) FROM Leader_Plan WHERE  ProjectType = '" + Session["P_T"] + "'  AND  PlanDate LIKE '%" + Session["year"] + "%' ";
     using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings"ythzhConnectionString"].ConnectionString))
        {
          SqlCommand scomm = new SqlCommand(Plan_Count, conn);
           
            try
            {
                conn.Open();
               int newProdID = (Int32)cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
      

  2.   

    datareader,怎么用,能写具体的吗?
      

  3.   

    int rowCount = int.Parse(scomm.ExecuteScalar());
      

  4.   


    DataSet ds = (new MTK.BLL.MRP()).GetMainList(1, 1, 1, 0, 1, PageSize);
    int number= ds.Tables[1].Rows.Count;
      

  5.   

    用dataset可以方便的找到数据量的大小!
      

  6.   


    scomm.ExecuteScalar()查询聚合函数结果用这个
      

  7.   

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings"ythzhConnectionString"].ConnectionString);string Plan_Count = "SELECT COUNT(*) FROM Leader_Plan WHERE (ProjectType = '" + Session["P_T"] + "') AND (PlanDate LIKE '%" + Session["year"] + "%')";
        
    SqlCommand scomm = new SqlCommand(Plan_Count, conn);int count = (Int32)cmd.ExecuteScalar();
      

  8.   


    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings"ythzhConnectionString"].ConnectionString);
    string Plan_Count = "SELECT COUNT(*) FROM Leader_Plan WHERE (ProjectType = '" + Session["P_T"] + "') AND (PlanDate LIKE '%" + Session["year"] + "%')";
        
    SqlCommand scomm = new SqlCommand(Plan_Count, conn);
    SqlDataAdapter da = new SqlDataAdapter(scomm);
    DataSet ds = new DataSet();
    da.Fill(ds);
    int number= ds.Tables[0].Rows.Count;tables的参数索引值是0还是1试试吧,我弄不清了
      

  9.   

    语句都写好了,就差执行了,DataReader和DataExecuteScalar
      

  10.   


    朋友  搞清楚  Tables[0].Rows.Count  和 count(*) 是查询的完全两回事Tables[0].Rows.Count 是结果集的记录条数  你这里永远等于1   因为肯定一条记录就是 xx (条记录)
    不是 5条记录的 5
      

  11.   

    尽管不建议使用DataSource控件,既然你用了,那何必还去CS中麻烦
    直接在aspx页面里加上
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                SelectCommand="SELECT COUNT(*) FROM Leader_Plan WHERE (ProjectType = @P_T) AND (PlanDate LIKE '%@year%')">       
            <SelectParameters>
                <asp:SessionParameter Name="P_T"  SessionField="P_T" DbType="String" DefaultValue="" />
                <asp:SessionParameter Name="year" SessionField="year" DbType="String" />
            </SelectParameters>
            </asp:SqlDataSource>
    就好了,CS中啥也不用写了PS:你的PlanDate如果是时间类型,用like查询效率很低的,用between..and 或者用year()先把datapart取出来再比较好一点
      

  12.   

    各位我这样写了,可是提示错误,说  ASPxDataView7重复绑定了,让我移走一个,我再ASPX页确实直接绑的ASPxDataView7的数据源,那我该怎么办呢?
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ythzhConnectionString"].ConnectionString);
            string Plan_Count = "SELECT COUNT(*) FROM Leader_Plan WHERE (ProjectType =  '" + Session["P_T"] + "') AND (PlanDate LIKE '%" + Session["year"] + "%')";
            
            SqlCommand scomm = new SqlCommand(Plan_Count, conn);
            conn.Open();
            int count = (Int32)scomm.ExecuteScalar();        if (count > 0)
            {
                ASPxDataView7.DataSource = SqlDataSource4 ;
                ASPxDataView7.DataBind();
                        }
            if (count == 0)
            { 
               ASPxDataView7.DataSource = null; 
                        }