YES-Win Winform开发框架 自定义查询方法并在表格中展示
方案一:BLL直接扩展方法
bll中新建一个search方法,把自己的条件参数都带上
源码如下:
C# 全选
public class bllLog : bllBaseUserCommon
{
public bllLog()
: base(typeof(sys_DataLog))
{
}
/// <summary>
/// 查询
/// </summary>
/// <param name="UserID"></param>
/// <param name="UserName"></param>
/// <param name="DataType"></param>
/// <param name="TableName"></param>
/// <param name="timeBegin"></param>
/// <param name="timeEnd"></param>
/// <returns></returns>
public DataTable Search(string UserID, string UserName, string DataType, string TableName, DateTime? timeBegin, DateTime? timeEnd)
{
// 查询sql语句
StringBuilder sqlBuilder = new StringBuilder();
// 查询参数
var cmds = new GZDBHelper.SqlParameterProvider();
sqlBuilder.AppendFormat("SELECT * FROM {0} WHERE 1=1", sys_DataLog._TableName);
if (!String.IsNullOrEmpty(UserID))
{
sqlBuilder.AppendFormat(" AND UserID = @UserID");
cmds.AddParameter("@UserID", SqlDbType.VarChar, UserID);
}
if (!String.IsNullOrEmpty(UserName))
{
sqlBuilder.AppendFormat(" AND UserName = @UserName");
cmds.AddParameter("@UserName", SqlDbType.VarChar, UserName);
}
if (!String.IsNullOrEmpty(DataType))
{
sqlBuilder.AppendFormat(" AND DataType = @DataType");
cmds.AddParameter("@DataType", SqlDbType.VarChar, DataType);
}
if (!String.IsNullOrEmpty(TableName))
{
sqlBuilder.AppendFormat(" AND DataType = @TableName");
cmds.AddParameter("@TableName", SqlDbType.VarChar, TableName);
}
if (timeBegin != null)
{
sqlBuilder.AppendFormat(" AND LogTime>=@LogTime1");
cmds.AddParameter("@LogTime1", SqlDbType.Date, timeBegin.Value.Date);
}
if (timeEnd != null)
{
sqlBuilder.AppendFormat(" AND LogTime<@LogTime2");
// 结束时间,存在 23:59秒
cmds.AddParameter("@LogTime2", SqlDbType.Date, timeEnd.Value.Date.AddDays(1));
}
sqlBuilder.AppendLine(" ORDER BY LogTime DESC");
DataTable data = dal.DBHelper.GetTable(sqlBuilder.ToString(), sys_DataLog._TableName, cmds);
return data;
}
}
设计查询界面
查询按钮点击方法
C# 全选
private void btn_Refresh_Click(object sender, EventArgs e)
{
DataTable dt1 = bll.Search(txtUserID.Text, txt_UserName.Text, txtDataType.Text, txtTableName.Text, txtTime1.DateTime, txtTime2.DateTime);
gc_Business.DataSource = dt1;
}
效果如下
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YESWinform 管理员