功能要求
制作步骤介绍
- 创建主页面
- 创建查询页面
- 实现搜索功能
- 搜索功能进阶(判断非空)
- 实现右键菜单删除功能
- 新增功能页面
制作过程
一.主页面

详细步骤
- 将各类控件放入窗口,并修改摆放
- 双击 “确定” 按钮
- 在 “确定” 按钮内获取当前选择的按钮,并弹出对应的窗口
代码展示
private void button1_Click(object sender, EventArgs e)
{
if (rb_delete.Checked)
{
MessageBox.Show("aaa");
}
else if (rb_add.Checked)
{
AddedForm a = new Added ();
a.Show();
}
else if (rb_change.Checked)
{
MessageBox.Show("CCC");
}
else if (rb_select.Checked)
{
SelectForm a = new Select();
a.Show();
}
}
二.查询窗口

详细步骤
1. 完成控件的排放
2. 通过listView 控件的 Columns 属性添加 Details 模式的列
3. 将listView 控件的 Dock 属性设置成Right
4. 连接数据库,并将数据库内的内容导入 listView
连接代码展示
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=QQ;Integrated Security=True");
string sql = "select * from BaseInfo;";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
while (result.Read())
{
string tempName = result["NickName"].ToString();
string tempAge = result["qqid"].ToString();
string temppre = result["Province"].ToString();
ListViewItem item = new ListViewItem(tempName, 0);
listView1.Items.Add(item);
item.SubItems.Add(tempAge);
item.SubItems.Add(temppre);
}
conn.Close();
5. 实现搜索功能(可以通过QQ号或昵称搜索)
- 刷新 listView 控件内容(通过 Clear( ) 方法)
- 获取输入框输入内容
- 在数据库内查询搜索内容
- 搜索进阶(判断非空)
搜索功能代码展示
listView1.Items.Clear();
string ww = "";
int ww1 = 0;
try { ww1 = Convert.ToInt32(textBox1.Text); }
catch { ww = textBox1.Text; }
String sql2 = "select * from BaseInfo where nickname = '"+ww+"' or qqid= "+ww1+";";
conn.Open();
SqlCommand cmd = new SqlCommand(sql2, conn);
try
{
SqlDataReader result = cmd.ExecuteReader();
result.Read();
string tempName = result["NickName"].ToString();
string tempAge = result["qqid"].ToString();
string temppre = result["Province"].ToString();
ListViewItem item = new ListViewItem(tempName);
listView1.Items.Add(item);
item.SubItems.Add(tempAge);
item.SubItems.Add(temppre);
conn.Close();
}
catch {
conn.Close();
MessageBox.Show("请输入内容!");
}
6. 实现右键删除菜单功能
- 双击contextMenuStrip 控件,删除键
- 删除通过QQ号进行删除,将选中的行的qqid列转换成int类型
- 在数据库内删除
- 刷新 listView 控件内容,再展示
右键删除功能代码展示
int dtName = Convert.ToInt32(listView1.SelectedItems[0].SubItems[1].Text.ToString());
conn.Open();
String sql = "delete from BaseInfo where qqid='" + dtName + "';";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
listView1.Items.Clear();
aaa();
三.新增窗口

详细步骤
1. 完成控件排放
2. 双击 “确定” 按钮
3. 获取两个输入框内的内容
4. 判断输入框是否为空
5. 如果不为空,则在数据库内新增一个数据
6. 提示新增成功,并关闭当前窗口
代码展示
string name = textBox1.Text.Trim();
string id = textBox2.Text.Trim();
if (!string.IsNullOrEmpty(name))
{
try
{
int qqid = Convert.ToInt32(id);
String sql = "insert into BaseInfo(qqid,nickname) values(" + id + ",'" + name + "');";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加成功");
this.Close();
}
catch
{
if (!string.IsNullOrEmpty(id)) { MessageBox.Show("请输入数字"); }
else { MessageBox.Show("请输入QQ号"); }
}
}
else
{
MessageBox.Show("QQ昵称为空!");
}