C# WinForm界面上实现按条件检索数据
2012-12-19 22:56
573 查看
实现步骤:
1.定义事件
2.定义方法
3.完善步骤2中的方法
1)在步骤2中的参数方法,定义参数方法,主要获取界面查询条件
2)实现在步骤2中的数据SQL查询语句方法
实例操作如下:
1.定义事件
btnSelectUserList.Click += btnSelectUserList_Click;
2.定义方法
void btnSelectUserList_Click(object sender, EventArgs e)
{
SelectUserList();
}
3.完善步骤2中的方法
1)在步骤2中的参数方法,定义参数方法,主要获取界面查询条件
2)实现在步骤2中的数据SQL查询语句方法
下面是一个数据库操作的基础类(DBHelper)的一个方法
杂记:下面演示的是通过消息请求反应机制,按检索消息传过来的数据列表(用户名,创建时间的阶段).
1.定义事件
2.定义方法
3.完善步骤2中的方法
1)在步骤2中的参数方法,定义参数方法,主要获取界面查询条件
2)实现在步骤2中的数据SQL查询语句方法
实例操作如下:
1.定义事件
btnSelectUserList.Click += btnSelectUserList_Click;
2.定义方法
void btnSelectUserList_Click(object sender, EventArgs e)
{
SelectUserList();
}
3.完善步骤2中的方法
private void SelectUserList() { UserService userService = new UserService(); DataTable userTable = userService.GetUserList(GetWhereUser()); bindingSource.DataSource = userTable; gridControl1.DataSource = bindingSource; }
1)在步骤2中的参数方法,定义参数方法,主要获取界面查询条件
private string GetWhereUser() { string strWhere = string.Empty; if (txt_UserName.Text.Trim() != "") strWhere += string.Format(" and Users.UserName like '%{0}%'", txt_UserName.Text.Trim()); if (this.time_LoginStart.Checked && this.time_LoginEnd.Checked) strWhere += string.Format(" and Users.DateCreated between '{0}' and '{1}'", this.time_LoginStart.Text, this.time_LoginEnd.Text); if (this.time_LoginStart.Checked) strWhere += string.Format(" and Users.DateCreated >= '{0}'", this.time_LoginStart.Text); if (this.time_LoginEnd.Checked) strWhere += string.Format(" and Users.DateCreated <= '{0}'", this.time_LoginEnd.Text); return strWhere; }
2)实现在步骤2中的数据SQL查询语句方法
/// <summary> /// 检索本地用户列表 /// </summary> /// <param name="strWhere"></param> /// <returns></returns> public DataTable GetUserList(string strWhere) { DataTable table = new DataTable(); try { string strSql = string.Format(@"select Users.UserId,Users.UserName,users.AuthenticationMode,users.RoleId,users.DisplayName,users.Email, Users.PhoneNumber,users.Status,users.DateLastLogon,users.CreatedBy,users.DateModifed from Users where users.Status=1"); strSql += strWhere; table = DataBase.Default.ExecuteDataTable(strSql, CommandType.Text); } catch(Exception ex) { throw new Exception("" + ex.Message + ex.StackTrace); } return table; }
下面是一个数据库操作的基础类(DBHelper)的一个方法
public DataTable ExecuteDataTable(string strSql, CommandType commandType) { DataTable table = new DataTable(); try { table = ExecuteDataTable(strSql, null, true); } catch (Exception ex) { log.Error("DB:" + GetKey() + Environment.NewLine + "SQL:" + strSql + Environment.NewLine + ex); throw new Exception("错误信息:" + ex.Message + ex.StackTrace); } return table; }
杂记:下面演示的是通过消息请求反应机制,按检索消息传过来的数据列表(用户名,创建时间的阶段).
private void SelectUserList() { //string str = "UserName '{0}%'"; //str = string.Format(str, txt_UserName.Text.Trim()); List<UserInfo> userInfoList = message.UserInfoList; if (!string.IsNullOrEmpty(txt_UserName.Text.Trim())) { userInfoList = message.UserInfoList.FindAll(c => c.UserName.StartsWith(txt_UserName.Text.Trim())); } if (this.time_LoginStart.Checked) userInfoList = userInfoList.FindAll(c => c.DateCreated.Value > time_LoginStart.Text.ToDateTime()); if (this.time_LoginEnd.Checked) userInfoList = userInfoList.FindAll(c => c.DateCreated.Value < time_LoginEnd.Text.ToDateTime()); bindingSource.DataSource = userInfoList; gridControl1.DataSource = bindingSource; gridView1.RefreshData(); return; }
相关文章推荐
- C#Winform内存加载多条件查询数据
- C#实现Winform间的数据交互的三种方法
- C#实现Winform窗口间数据交互的三种方法介绍
- 如何使数据绑定的控件实现“上一条”、“下一条”、“第一条”和“最后一条”的功能?(WinForm, C#)
- C# Winform实现炫酷的透明动画界面(转载)
- c# winform 欢迎界面时加载数据
- 【C#利用后台动态加载数据】Winform“防界面卡死”
- C# WinForm 实现增删改查等功能(Access版) 系列之八-导出数据到Excel
- c#winform datagridview的数据绑定和行的增删改实现
- (转)【C#利用后台动态加载数据】Winform“防界面卡死”
- C#winform 实现 QQ2009游戏界面
- C#利用反射技术实现winform界面动态存储
- C#实现Winform间的数据交互的三种方法
- c# 用反射实现winform界面动态存储理论知识
- c# 模仿 vue 实现 winform 的数据模型双向绑定
- C# WinForm实现UDP发送和接收数据
- 【C#利用后台动态加载数据】Winform“防界面卡死”
- C# WinForm 实现增删改查等功能(Access版) 系列之八-导出数据到Excel
- C#在winform中操作数据库,实现数据增删改查
- C#实现SQL全库检索数据-比较使用DataReader与DataAdapter+Datatable效率,差距惊人!