我有两个想法:
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、托个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);接着怎么写能取出来?
请指教,哪个方法好呢?
解决方案 »
- JS如何写入cookie?在其他页面后台调用
- 使用iframe 取不到session 的值
- checkbox的一些问题
- !!!急,在線等,請問如何實現在 Frame 的卷軸上加一個用於隱藏 Frame 的按鈕?!!!
- 在ASPxGrid活者DataGrid中,单击某一个模板列(按钮名称为下载),如何不进入编辑状态?
- &&&&&&&&&&&&50分,,问个很简单的问题?URL参数判断问题?&&&&&&&&&&&&&&
- Server.Transfer 用这个方法传参数和转向页面。google 和百度能搜到我要转向的页面吗?爬虫可以找到我要转向到的网站页面吗?
- 客户端与服务段的交互
- 问ajaxpro的一个获取值的小问题
- 怎么把数据库里绑定的字段显示到TextBox中
- js请求web服务的webconfig设置。
- mvc的参数传递
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);
}
}
DataSet ds = (new MTK.BLL.MRP()).GetMainList(1, 1, 1, 0, 1, PageSize);
int number= ds.Tables[1].Rows.Count;
scomm.ExecuteScalar()查询聚合函数结果用这个
SqlCommand scomm = new SqlCommand(Plan_Count, conn);int count = (Int32)cmd.ExecuteScalar();
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试试吧,我弄不清了
朋友 搞清楚 Tables[0].Rows.Count 和 count(*) 是查询的完全两回事Tables[0].Rows.Count 是结果集的记录条数 你这里永远等于1 因为肯定一条记录就是 xx (条记录)
不是 5条记录的 5
直接在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取出来再比较好一点
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;
}