增加界面中的搜索功能

会员资料管理界面(VIPManager.cs):

详细代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 会员管理系统
{
public partial class VIPManager : Form
{
public VIPManager()
{
InitializeComponent();
} //连接字符串 获取配置文件里的连接路径,多次需要调用,放在外面方便
static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
//窗体运行自动加载
private void VipManager_Load(object sender, EventArgs e)
{
//刷新数据
Refresh();
cmbforfieldSelecting.Text = "全局搜索";
cmbforfieldSelecting.Items.Add("全局搜索");
cmbforfieldSelecting.Items.Add("编号");
cmbforfieldSelecting.Items.Add("名字");
cmbforfieldSelecting.Items.Add("性别");
cmbforfieldSelecting.Items.Add("年龄");
cmbforfieldSelecting.Items.Add("地址");
cmbforfieldSelecting.Items.Add("电话");
} //写一个刷新数据的方法(跟查看数据一样)
public void Refresh(bool isAdded = false)
{
//查询数据库字符串
string sql = String.Format("select vId '{0}',vName '{1}',vGender '{2}',vAge '{3}',vAddress '{4}',vPhone '{5}' from VipInformation", "编号", "名字", "性别", "年龄", "地址", "电话");
//连接数据库对象
SqlConnection conn = new SqlConnection(connStr);
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//创建表对象
System.Data.DataTable dt = new System.Data.DataTable();
//创建数据库填充操作对象(语句)
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//把数据填充进dt表中
sda.Fill(dt);
//指定dgvManager控件的数据源:dt
dgvManager.DataSource = dt; //if (isAdded)
//{
// if (dt.Rows.Count > 0)
// dgvManager.Rows[0].Selected = false;
// dgvManager.Rows[dt.Rows.Count - 1].Selected = true;
//}
} //刷新数据界面
private void btnView_Click(object sender, EventArgs e)
{
//刷新数据
Refresh();
} //添加数据
private void btnAdd_Click(object sender, EventArgs e)
{
//判断文本框是否为空,提示数据完整性
if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "")
{
MessageBox.Show("数据不能为空,请填写齐全");
return;
}
//插入数据库字符串
string sql = string.Format("insert into VipInformation values('{0}','{1}',{2},'{3}','{4}')",txtName.Text.Trim(),txtGender.Text.Trim(),txtAge.Text.Trim(),txtAddress.Text.Trim(),txtPhone.Text.Trim());
//连接数据库对象
SqlConnection conn = new SqlConnection(connStr);
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//创建表对象
System.Data.DataTable dt = new DataTable();
//创建数据库填充操作对象(语句)
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//把数据填充进dt表中
sda.Fill(dt);
//指定dgvManager控件的数据源:dt
dgvManager.DataSource = dt;
//刷新数据
Refresh();
} //删除数据
private void btnDelete_Click(object sender, EventArgs e)
{
//使用sql删除语句,where 1=1 就是没有条件,等于全部数据删除
string sql = "delete from VipInformation where 1=1";
//如果选中某行则执行
if (dgvManager.CurrentRow.Selected)
{
sql = sql + " and vid=" + Convert.ToInt32(dgvManager.CurrentRow.Cells[].Value.ToString());
}
int n = ;
//创建连接数据库对象
SqlConnection conn = new SqlConnection(connStr);
//创建操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//打开数据库
conn.Open();
//取得ExecuteNonQuery返回的受影响行数,无影响则为0
n = cmd.ExecuteNonQuery();
if (n == )
{
MessageBox.Show("删除操作失败!不存在的ID");
conn.Close();
return;
}
else if (n > )
{
MessageBox.Show("删除操作成功!");
}
//关闭数据库连接
conn.Close();
//刷新数据界面
Refresh();
} //修改数据
private void btnSave_Click(object sender, EventArgs e)
{
if (txtName.Text == "" || txtGender.Text == "" || txtAge.Text == "" || txtAddress.Text == "" || txtPhone.Text == "")
{
MessageBox.Show("所提供的数据不完整,请填写完整数据");
return;
}
int n = ;
//更新SQL语句
string sqlupdate = "update VipInformation set vName='" + txtName.Text + "',vgender='" + txtGender.Text + "',vage=" + txtAge.Text + ",vaddress='" + txtAddress.Text + "',vphone='" + txtPhone.Text + "' where vid='" + dgvManager.CurrentRow.Cells[].Value.ToString() + "'";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sqlupdate, conn);
conn.Open();
n = cmd.ExecuteNonQuery();
if (n == )
{
MessageBox.Show("修改操作失败!");
conn.Close();
return;
}
else if (n > )
{
MessageBox.Show("修改操作成功!");
}
conn.Close();
Refresh();
} //点击dgvManager在文本框上显示
private void dgvManager_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtName.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtGender.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtAge.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtAddress.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
txtPhone.Text = dgvManager.CurrentRow.Cells[].Value.ToString();
} string selectedValue;
//事件索引改变时触发
private void cmbforfieldSelecting_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelected = cmbforfieldSelecting.Text;
switch (strSelected)
{
case "全局搜索":
selectedValue = "全局搜索";
break;
case "编号":
selectedValue="vid";
break;
case "名字":
selectedValue = "vname";
break;
case "性别":
selectedValue = "vgender";
break;
case "年龄":
selectedValue = "vage";
break;
case "地址":
selectedValue = "vaddress";
break;
case "电话":
selectedValue = "vphone";
break;
default:
selectedValue = "全局搜索";
break;
}
} private void txtDataforQuery_TextChanged(object sender, EventArgs e)
{
string sql = "";
if (txtDataforQuery.Text.Trim() == "")
{
//执行查询语句
sql = "select * from VipInformation";
}
else if (cmbforfieldSelecting.Text.Trim() == "全局搜索" || selectedValue == "全局搜索")
{
//全字段搜索
sql = "select * from VipInformation where vName like '%" + txtDataforQuery.Text.Trim() + "%' or vgender like '%" + txtDataforQuery.Text.Trim() + "%' or vage like '%" + txtDataforQuery.Text.Trim() + "%' or vaddress like '%" + txtDataforQuery.Text.Trim() + "%' or vphone like '%" + txtDataforQuery.Text.Trim() + "%'";
}
else if (selectedValue == "vid" || selectedValue == "vname" || selectedValue == "vgender" || selectedValue == "vage" || selectedValue == "vaddress" || selectedValue == "vphone")
{
//通过相应的字段进行搜索
sql = "select * from VipInformation where " + selectedValue + " like '%" + txtDataforQuery.Text.Trim() + "%'";
} SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dgvManager.DataSource = dt;
conn.Close();
} private void btnBack_Click(object sender, EventArgs e)
{
VIPMain vmain = new VIPMain();
vmain.Show();
this.Hide();
} private void btnClose_Click(object sender, EventArgs e)
{
VIPLog vpl = new VIPLog();
vpl.GetExitTime();
vpl.AddMsg();
//彻底的退出
System.Environment.Exit();
} }
}

日志查看(VIPLog.cs):

详细代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration; namespace 会员管理系统
{
public partial class VIPLog : Form
{
public VIPLog()
{
InitializeComponent();
}
//连接数据库字符串多次调用,放在外面省代码量
public static string connstr = ConfigurationManager.ConnectionStrings["str"].ToString(); private void btnClose_Click(object sender, EventArgs e)
{
//获取当前时间
GetExitTime();
//添加当前的用户信息到日志中
AddMsg();
//退出程序
System.Environment.Exit();
} private void btnBack_Click(object sender, EventArgs e)
{
VIPMain vm = new VIPMain();
vm.Show();
this.Hide();
} private void VIPLog_Load(object sender, EventArgs e)
{
//AddMsg();
ShowMsg();
}
//写一个窗体加载自动从数据库获取数据并显示在datagridview的方法
public void ShowMsg()
{
//连接数据库对象
SqlConnection conn = new SqlConnection(connstr);
string sql = "select [id] 编号,vName 名字,situation 状态,[time] 登录或退出时间,vtype 权限 from [log]";
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql, conn);
//创建表对象
DataTable dt = new DataTable();
//创建数据库填充操作对象(语句)
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//把数据填充进dt表中
sda.Fill(dt);
//指定dgvManager控件的数据源:dt
dgvLog.DataSource = dt;
}
//写一个窗体加载(或者退出)自动添加当前用户登录信息进数据库的方法
public void AddMsg()
{
//连接数据库对象
SqlConnection conn = new SqlConnection(connstr); string sql = string.Format("insert into [Log](vName,situation,time) values('{0}','{1}','{2}')",VIPLogin.uid,VIPLogin.situation,VIPLogin.time);
//操作数据库对象
SqlCommand cmd = new SqlCommand(sql,conn);
//打开数据库连接
conn.Open();
//执行操作数据库对象并返回受影响条数
int n = cmd.ExecuteNonQuery();
//关闭数据库连接
conn.Close();
}
//写一个获取当前系统时间的方法(退出时间)
//退出时顺手修改登录状态
public void GetExitTime()
{
VIPLogin.time = DateTime.Now;
VIPLogin.situation = "退出";
} //日志查询
private void btnLogQuery_Click(object sender, EventArgs e)
{
//初始化 showSql语句
string sql = "";
//如果文本框txtQuery 输入的内容为空值
if (txtLogQuery.Text.Trim() == "")
{
//显示所有数据
sql = "select * from [log]";
}
else
{
//否则 进行 全局搜索 将文本框里的内容与 各字段 进行 匹配
sql = "select * from [Log] where [id] like '%" + txtLogQuery.Text.Trim() + "%' or vname like '%" + txtLogQuery.Text.Trim() + "%' or situation like '%" + txtLogQuery.Text.Trim() + "%' or [time] like '%" + txtLogQuery.Text.Trim() + "%' or vtype like '%" + txtLogQuery.Text.Trim() + "%'";
}
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dgvLog.DataSource = dt;
} }
}

C#_会员管理系统:开发六(数据搜索)的更多相关文章

  1. C#_会员管理系统:开发七(用户分类)

    登录界面(VIPLogin.cs)详细代码: using System; using System.Collections.Generic; using System.ComponentModel; ...

  2. C#_会员管理系统:开发八(权限分配)

    数据库设计 这里由于增加了普通用户权限值,我们需要对数据库结构稍作修改.这里在MovieAccount表中增加4列内容 分别用于 RightFManager       判断普通用户管理界面权限   ...

  3. C#_会员管理系统:开发四(日志查看)

    新建一个日志查看窗体: 日志需要的登录时间和登录状态信息由用户刚登录程序时就提供,所以在登录窗体(VIPLogin.cs)中添加代码: //定义一个全局变量 Uid; //用于获取登录成功后的用户名 ...

  4. C#_会员管理系统:开发三(修改密码)

    为以后多个功能界面考虑,新增一个主界面: 主界面如下: 主界面(VIPMain.cs)详细代码如下: using System; using System.Collections.Generic; u ...

  5. C#_会员管理系统:开发二(会员资料管理界面的‘增删改查’)

    会员资料管理界面: 新建一个窗体,窗体界面和控件如下: 窗体中的控件dgvManager更改FullRowSelect属性(点击选中效果)为:FullRowSelect 会员资料管理界面窗体的详细代码 ...

  6. C#_会员管理系统:开发一(用户登录)

    首先创建数据库: [Vip] 创建三张表: 分别是: [VipInformation](会员信息) [Log](日志) [VipAccount](账户权限) 详细语句: --创建数据库[Vip] cr ...

  7. C#_会员管理系统:开发五(用户注册)

    创建一个新的用户注册窗体(VIPRegistration.cs): 用户注册窗体(VIPRegistration.cs)详细代码如下: using System; using System.Colle ...

  8. C#_会员管理系统

    https://www.cnblogs.com/start-from-scratch/p/5420588.html

  9. 会员管理系统的设计和开发(2)-- RDLC报表的设计及动态加载

    在上篇<会员管理系统的设计和开发(1)>介绍了关于会员系统的一些总体设计思路和要点,经过一段时间开发,软件终于完成并发布.在这期间,碰到了不少技术难点,并积累了不少开发心得和经验,本篇继续 ...

随机推荐

  1. android五种布局模式

    Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...

  2. [LeetCode]题解(python):134-Gas Station

    题目来源: https://leetcode.com/problems/gas-station/ 题意分析: 在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油.现在有一辆无限容量的车,它从 ...

  3. ubuntu vim终端编辑命令

    一. VIM高亮 进入vim后,在普通模式下输入如下命令,开启php代码高亮显示   :syntax enable   :source $VIMRUNTIME/syntax/php.vim   二. ...

  4. PROTEL99生成GERBER的操作说明

    GBL BOTTOM LAYER(底层布线图)GBO BOTTOM OVERLAYER(底层丝印层)GBP BOTTOM PASTE LAYER(底层锡膏层)GBS BOTTOM SOLDER MAS ...

  5. python defaultdict 类型

    在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collections.defaultdict()经常被用到.主要说说这个东西. 综述: 这里的defaultd ...

  6. js 获取前天、昨天、今天、明天、后天的时间

    js 获取前天.昨天.今天.明天.后天的时间 2011-05-19 21:03   <html><head><meta http-equiv="Content- ...

  7. poj 2375 Cow Ski Area bfs

    这个题目用tarjan找联通块,缩点,然后统计出入度为0的点理论上是可行的,但问题是会暴栈.考虑到这个题目的特殊性,可以直接用一次bfs找到数字相同且联通的块,这就是一个联通块,然后缩点,统计出入度即 ...

  8. boost::string or boost::regex

    有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...

  9. VCS引起的oracle数据库异常重新启动一例

    1. 环境描写叙述 操作系统版本号:SUSE Linux Enterprise Server 10 sp2 (x86_64) 数据库版本号:Oracle 11.1.0.7.16 VCS版本号:5.1 ...

  10. Js 返回页面 or 跳转页面

    跳出 iframe 在当前页跳转, window.parent.frames.location.href=www.baidu.com" 跳转页面 onclick="history. ...