C# 使用表达式从数据集获取记录
我想使用一些表达式从数据集中获取记录 这是我的数据集 这是我的数据集,我想根据punchDate获取两个日期之间的regords 示例:我想获取从C# 使用表达式从数据集获取记录,c#,.net,dataset,C#,.net,Dataset,我想使用一些表达式从数据集中获取记录 这是我的数据集 这是我的数据集,我想根据punchDate获取两个日期之间的regords 示例:我想获取从03-01-2012到25-01-2012之间的记录,这意味着它返回正确的记录 假设我只输入日期,如03-12-2011到25-01-2012意味着它返回错误 错误:源不包含数据行 我在此附上我的代码: protected void findbyexpression(string Expression) { try {
03-01-2012
到25-01-2012
之间的记录,这意味着它返回正确的记录
假设我只输入日期,如03-12-2011
到25-01-2012
意味着它返回错误
错误:源不包含数据行
我在此附上我的代码:
protected void findbyexpression(string Expression)
{
try
{
DataTable dt1 = new DataTable();
DataSet4TableAdapters.sp_getalltimesheetTableAdapter TA = new DataSet4TableAdapters.sp_getalltimesheetTableAdapter();
DataSet4.sp_getalltimesheetDataTable DS = TA.GetData();
DataTable DT = new DataTable();
DT = DS[0].Table;
DataRow[] foundRows;
foundRows = DT.Select(Expression);
if (DT.Rows.Count == 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('No records found for request query');</script>", false);
}
else
{
panheader.Visible = true;
dt1 = foundRows.CopyToDataTable();
Session["TaskTable"] = dt1;
grdvw.DataSource = Session["TaskTable"];
grdvw.DataBind();
}
}
catch (Exception e2)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('" + e2.Message + "');</script>", false);
}
}
受保护的void findbyexpression(字符串表达式)
{
尝试
{
DataTable dt1=新DataTable();
DataSet4TableAdapters.sp_getalltimesheetTableAdapter TA=新的DataSet4TableAdapters.sp_getalltimesheetTableAdapter();
DataSet4.sp_getalltimesheetDataTable DS=TA.GetData();
DataTable DT=新的DataTable();
DT=DS[0]。表;
DataRow[]foundRows;
foundRows=DT.Select(表达式);
如果(DT.Rows.Count==0)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),“temp”,“警报('未找到请求查询的记录');”,false);
}
其他的
{
panheader.Visible=true;
dt1=foundRows.CopyToDataTable();
会话[“任务表”]=dt1;
grdvw.DataSource=会话[“任务表”];
grdvw.DataBind();
}
}
捕获(异常e2)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),“temp”,“alert(“+e2.Message+”);”,false);
}
}
表达方式是:
string expression = "PunchDate >= '" + txtfromdate.Text + "' and PunchDate <= '" + txttodate.Text + "'";
findbyexpression(expression);
string expression=“PunchDate>=”+txtfromdate.Text+“”和PunchDate
dataview=dataset.Tables[表名称].DefaultView
dataview.RowFilter=“DATE>=”+startDate.ToSortDateString()+“”和日期您的查询没有正确选择记录。您可以选中此链接以选择范围之间的日期。使用这个条件
string expression = PunchDate < YourEndDate AND PunchDate >= your StartDate.
string expression=PunchDate=您的开始日期。
- 您的查询可以被Sql注入,请尝试使用参数化查询李>
- 如果可能,尝试将所有变量重命名为有意义的名称李>
感谢您的快速回复……现在我不明白错误所在。但在dataview中找不到记录。是否在数据集中的日期内添加筛选器??如果helpedI使用上面的表达式得到了结果,则将其标记为“答案”,但有细微的变化。”string expression=“PunchDate>=#“+txtfromdate.Text+”#和PunchDate谢谢你的回复我得到了结果。