QQ项目之五查找和添加好友
【摘要】
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Syste...
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Data.SqlClient; //引用命名空间
-
-
namespace MYQQ
-
{
-
/// <summary>
-
/// 查找和添加好友窗体;注意是周末大家要实现的大招,难度中难~.~
-
/// </summary>
-
//**********************第1步:在类中声明2个成员变量,用来存取数据*************************
-
public partial class SearchFriendForm : Form
-
{
-
private DataSet dataSet; // 数据集
-
private SqlDataAdapter dataAdapter; // 数据适配器
-
//*********************第1步实现完毕********************************************************
-
public SearchFriendForm()
-
{
-
InitializeComponent();
-
}
-
-
#region 第2步,窗体加载代码,填充数据集;
-
private void SearchFriendForm_Load(object sender, EventArgs e)
-
{
-
// 实例化数据集和数据适配器并填充,断点、try块捕获异常;
-
try
-
{
-
string sql = "SELECT Id, NickName, Age, Sex FROM Users"; //定义查询语句,如果不设计datagridView控件,则使用别名实现!
-
dataAdapter = new SqlDataAdapter(sql, DBHelper.conn); //实现小货车new
-
dataSet = new DataSet("MyQQ"); //临时仓库MyQQ
-
dataAdapter.Fill(dataSet, "Users"); //临时表Users
-
-
// 指定DataGridView的数据源
-
dgvBasicResult.DataSource = dataSet.Tables["Users"]; //给基本数据表格(dgvBasicResult)指定数据源
-
dgvAdvancedResult.DataSource = dataSet.Tables["Users"]; //给高级数据表格(dgvAdvancedResult)指定数据源
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
}
-
#endregion
-
-
#region 第3步 点击查找按钮时,查找符合条件的用户,p268
-
private void btnSearch_Click(object sender, EventArgs e)
-
{
-
if (tabSearch.SelectedIndex == 0) // 基本查找选显卡选中
-
{ //实现,将业务逻辑的粒度进行细分;
-
BasicallySearch(); //自定义方法,基本查找;写到类中,其他方法外,和其他方法是兄弟关系;
-
}
-
else // 高级查找选项卡选中
-
{
-
AdvancedSearch(); //自定义方法:高级查找;
-
}
-
}
-
/// <summary>
-
/// 这是基本查找方法..;需要4种sql语句;全部查找,加载时候已经实现;
-
/// 3种;两个都不选,会有提示框;选一个ID/Nickname/
-
/// </summary>
-
-
-
#endregion
-
-
#region 添加好友;整体框架
-
private void btnAdd_Click(object sender, EventArgs e)
-
{
-
int friendshipPolicyId = -1; // 对方的好友策略
-
int friendId = GetSelectedFriendId(); //子步骤1:获得选中的好友的Id;
-
if (friendId == -1)// 是否有可添加的好友
-
{
-
MessageBox.Show("请选择一个好友!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return;
-
}
-
else if (friendId == UserHelper.loginId)
-
{
-
MessageBox.Show("不能加自己为好友!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return;
-
}
-
else if (HasAdded(friendId)) //子步骤2:确认是否需要添加(是否已经是好友了)
-
{
-
MessageBox.Show("对方已在你的好友列表中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return;
-
}
-
//上面都是无效条件,除此之外,就是可以添加好友的情况;添加好友又分几种情况呢?
-
friendshipPolicyId = GetFriendshipPolicyId(friendId); //添加好友子步骤3,获得好友好友号码的好友策略
-
-
if (friendshipPolicyId == 3) // 3表示不允许任何人添加,在S2可以使用常量或枚举
-
{
-
MessageBox.Show("对方不允许任何人加他为好友!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return;
-
}
-
else if (friendshipPolicyId == 2) // 2:表示需要身份验证
-
{
-
int result = SendRequest(friendId); // 添加好友子步骤4:发验证消息
-
//可以再调出来一个窗体,进行传值.
-
if (result == 1)
-
{
-
MessageBox.Show("对方需要身份验证才能被加为好友,已发出请求!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
}
-
else
-
{
-
MessageBox.Show("添加失败,请稍候再试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
-
}
-
}
-
else if (friendshipPolicyId == 1) // 1:表示允许任何人添加为好友
-
{
-
int result = AddFriend(friendId); //添加好友子步骤5: 执行添加操作
-
if (result == 1)
-
{
-
MessageBox.Show("添加成功,请刷新好友列表!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
}
-
else
-
{
-
MessageBox.Show("添加失败,请稍候再试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
-
}
-
}
-
}
-
#endregion
-
// 当切换页时,把与查找结果相关的控件都隐藏
-
private void tabSearch_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
pnlBaseResult.Visible = false;
-
pnlAdvancedResult.Visible = false;
-
btnAdd.Visible = false;
-
btnBack.Visible = false;
-
}
-
-
// 控制精确查找选项是否可见;设置下面的隐藏可以见
-
private void rdoNicetySearch_CheckedChanged(object sender, EventArgs e)
-
{
-
if (rdoNicetySearch.Checked == true)
-
{
-
grpBaseCondition.Visible = true; //控制精确查找面板的可见
-
}
-
else
-
{
-
grpBaseCondition.Visible = false; //不可见
-
}
-
}
-
-
// 返回到上一步
-
private void btnBack_Click(object sender, EventArgs e)
-
{
-
pnlBaseResult.Visible = false; //结果面板,隐藏
-
pnlAdvancedResult.Visible = false; //高级查找面板,隐藏
-
-
pnlBaseCondition.Visible = true; // 基本查找可见;
-
pnlAdvancedCondition.Visible = true;//高级查找可见;
-
btnBack.Visible = false; //上一步按钮,隐藏
-
btnAdd.Visible = false; //添加,隐藏
-
}
-
-
// 关闭窗体
-
private void btnClose_Click(object sender, EventArgs e)
-
{
-
this.Close();
-
}
-
-
#region 第4步:基本查找
-
private void BasicallySearch()
-
{ //拼接Sql语句 // 查询语句的前半部分
-
string sql = "SELECT Id,NickName,Age,Sex FROM Users";
-
// 精确查找单选按钮的name:rdoNicetySearch
-
if (rdoNicetySearch.Checked == true)
-
{
-
if (txtLoginId.Text.Trim() == "" && txtNickName.Text.Trim() == "")
-
{
-
MessageBox.Show("还没有填查询条件呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return; //注意,只有return
-
}
-
// 按帐号查找
-
else if (txtLoginId.Text.Trim() != "" && txtNickName.Text.Trim() == "")
-
{
-
sql += string.Format(" WHERE Id={0}", int.Parse(txtLoginId.Text.Trim()));
-
}
-
// 按昵称查找
-
else if(txtNickName.Text.Trim()!="" && txtLoginId.Text.Trim()=="")
-
{
-
sql += string.Format(" WHERE NickName like '%{0}%'", txtNickName.Text.Trim()); //like:模糊
-
}
-
else{ //既然,账号和昵称必须一致;
-
sql+=string.Format(" where Id={0} and NickName like '%{1}'",int.Parse(txtLoginId.Text.Trim()),txtNickName.Text.Trim());
-
}
-
}
-
// 重新填充DataSet
-
dataSet.Tables[0].Clear(); //清空临时库中第一个表(即Users表)的数据
-
dataAdapter.SelectCommand.CommandText = sql; //设置小货车查询小二的命令;
-
dataAdapter.Fill(dataSet, "Users"); //使用小货车,重新填充数据岛Users表中;
-
-
// 设置控件可见的属性
-
// 调整显示结果的panel的位置,让它和显示条件的Panel的位置相同
-
pnlBaseResult.Location = pnlBaseCondition.Location;
-
// 使显示结果的panel可见
-
pnlBaseResult.Visible = true;
-
btnAdd.Visible = true; //“加为好友”按钮可见
-
btnBack.Visible = true; //“上一步”按钮可见
-
}
-
#endregion
-
//1.声明2个成员变量;
-
//2.窗体加载代码;
-
//3.点击查找,出现两种情况;
-
//4.基本查找;
-
//5.高级查找;
-
-
#region 第5步 高级查找
-
private void AdvancedSearch()
-
{
-
string sql = "SELECT Id,NickName,Age,Sex FROM Users ";
-
string ageCondition = ""; // 年龄条件
-
string sexCondition = cboSex.Text; // 性别条件
-
-
// 确定年龄的范围
-
switch (cboAge.SelectedIndex) //选择项索引,从0开始;0:不限;
-
{
-
case 1:
-
ageCondition = " Age>=0 AND Age<10";
-
break;
-
case 2:
-
ageCondition = " Age>=10 AND Age<20";
-
break;
-
case 3:
-
ageCondition = " Age>=20 AND Age<30";
-
break;
-
case 4:
-
ageCondition = " Age>=30 AND Age<40";
-
break;
-
case 5:
-
ageCondition = " Age>=40 AND Age<50";
-
break;
-
case 6:
-
ageCondition = " Age>=50";
-
break;
-
default:
-
ageCondition = "";
-
break;
-
}
-
-
if (ageCondition == "" && sexCondition == "")
-
{
-
MessageBox.Show("还没有选择查询条件呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
return;
-
}
-
else
-
{//否则,有3种情况。
-
if (ageCondition != "" && sexCondition == "") //只有年龄
-
{
-
sql += string.Format(" WHERE {0}", ageCondition);
-
}
-
else if (ageCondition == "" && sexCondition != "") //只有性别
-
{
-
sql += string.Format(" WHERE Sex='{0}'", sexCondition);
-
}
-
else
-
{ //两个条件都有
-
sql += string.Format(" WHERE {0} AND Sex='{1}'", ageCondition, sexCondition);
-
}
-
}
-
-
// 开始查找
-
dataAdapter.SelectCommand.CommandText = sql;
-
dataSet.Tables[0].Clear();
-
dataAdapter.Fill(dataSet, "Users");
-
-
// 设置控件的属性
-
pnlAdvancedResult.Location = pnlAdvancedCondition.Location;
-
pnlAdvancedResult.Visible = true;
-
btnAdd.Visible = true;
-
btnBack.Visible = true;
-
}
-
#endregion
-
/// <summary>
-
/// 添加好友子步骤1:获得选中的好友的Id
-
/// </summary>
-
private int GetSelectedFriendId()
-
{
-
int friendId = -1; // 好友的号码
-
-
if (tabSearch.SelectedIndex == 0) // 基本查找
-
{
-
// 没有选中任何一行
-
if (dgvBasicResult.SelectedRows.Count == 0)
-
{
-
MessageBox.Show("请选择一个好友!", "操作", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
}
-
// 确保第一个单元格有值;Cells:单元格;
-
else if(dgvBasicResult.SelectedRows[0].Cells[0] != null)
-
{
-
// 获得DataGridView中选中的行的第一个单元格的值
-
friendId = int.Parse(dgvBasicResult.SelectedRows[0].Cells[0].Value.ToString());
-
}
-
}
-
// 高级查找
-
else
-
{
-
if (dgvAdvancedResult.SelectedRows.Count == 0)
-
{
-
MessageBox.Show("请选择一个好友!", "操作", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
}
-
else if (dgvAdvancedResult.SelectedRows[0].Cells[0] != null)
-
{
-
friendId = int.Parse(dgvAdvancedResult.SelectedRows[0].Cells[0].Value.ToString());
-
}
-
}
-
-
return friendId; //切记:返回好友ID
-
}
-
-
/// <summary>
-
/// 确认对方能否被加为好友
-
/// </summary>
-
private int GetFriendshipPolicyId(int friendId) //好友号码,作为参数过去10009
-
{
-
int friendshipPolicyId = 1; // 好友策略
-
string sql = "SELECT FriendshipPolicyId FROM Users WHERE Id=" + friendId;
-
try
-
{
-
SqlCommand command = new SqlCommand(sql, DBHelper.conn);
-
DBHelper.conn.Open();
-
-
friendshipPolicyId = Convert.ToInt32(command.ExecuteScalar());
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine(ex.Message);
-
}
-
finally
-
{
-
DBHelper.conn.Close();
-
}
-
return friendshipPolicyId;
-
}
-
//*********************************子步骤3结束了*************************
-
/// <summary>
-
/// 添加好友子步骤2
-
/// </summary>
-
private bool HasAdded(int friendId) //把刚才获得的好友号码,作为参数,给传过来.
-
{
-
int result = 0; // 查找结果
-
bool returnValue; // 返回值
-
string sql = string.Format("SELECT COUNT(*) FROM Friends WHERE HostId={0} AND FriendId={1}",UserHelper.loginId, friendId);
-
try
-
{
-
SqlCommand command = new SqlCommand(sql, DBHelper.conn);
-
DBHelper.conn.Open();
-
result = Convert.ToInt32(command.ExecuteScalar());
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine(ex.Message);
-
}
-
finally
-
{
-
DBHelper.conn.Close();
-
}
-
-
// 已有记录存在,?: 相当于if else,三元运算符;
-
returnValue = result > 0 ? true : false;
-
return returnValue;
-
}
-
//***************************************子步骤2,结束了****************************************
-
/// <summary>
-
/// 添加好友,第5步
-
/// </summary>
-
private int AddFriend(int friendId)//朋友号码:10016,加过去,作为10000的朋友,直接加过去
-
{
-
int resunlt = 0; // 操作结果
-
string sql = string.Format("INSERT INTO FRIENDS (HostId, FriendId) VALUES ({0},{1})",UserHelper.loginId, friendId);
-
-
try
-
{
-
// 执行添加操作
-
SqlCommand command = new SqlCommand(sql, DBHelper.conn);
-
DBHelper.conn.Open();
-
resunlt = command.ExecuteNonQuery();
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine(ex.Message);
-
}
-
finally
-
{
-
DBHelper.conn.Close();
-
}
-
return resunlt;
-
}
-
//*************************************子步骤5,结束了*************************
-
/// <summary>
-
/// 添加好友子步骤4:发出添加好友请求;接触到一个表:消息表;
-
/// </summary>
-
private int SendRequest(int friendId) //10009
-
{
-
int resunlt = 0; // 操作结果
-
string sql = string.Format("INSERT INTO Messages(FromUserId, ToUserId, MessageTypeId, MessageState) VALUES ({0},{1},{2},{3})",
-
UserHelper.loginId, friendId, 2, 0);//2:添加好友的消息;0:消息未读;
-
//10000加10009
-
try
-
{
-
// 执行添加操作
-
SqlCommand command = new SqlCommand(sql, DBHelper.conn);
-
DBHelper.conn.Open();
-
resunlt = command.ExecuteNonQuery();
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine(ex.Message);
-
}
-
finally
-
{
-
DBHelper.conn.Close();
-
}
-
return resunlt;
-
}
-
-
private void pnlBaseCondition_Paint(object sender, PaintEventArgs e)
-
{
-
-
}
-
//***************************************************子步骤4结束了**********************
-
}
-
}
视频课堂https://edu.csdn.net/course/play/7621
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/50486828
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)