winfin-图片(显示,分页,名称,类别)查询

前言

我的文章相对于来说,理论的东西是很少,代码的实践是很多,如果有看不明白的朋友,可以到网上查看一下其它的一些资料,也是可以的,今天我写的这篇文章,虽然简短,但是很精悍。

(1)数据库(MySQL)

(2)实体类(Model)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 图片信息
    /// </summary>
    public class Phone
    {
        /// <summary>
        /// 无参构造
        /// </summary>
        public Phone()
        {

        }

        /// <summary>
        /// 有参构造
        /// </summary>
        /// <param name="phoneId">相册ID</param>
        /// <param name="phoneName">相册名称</param>
        /// <param name="phoneClass">相册类型</param>
        /// <param name="phonePath">相册路径</param>
        /// <param name="phoneReak">相册简介</param>
        /// <param name="data">相册入存时间</param>
        public Phone(string phoneId, string phoneName, string phoneClass, string phonePath, string phoneState,string phoneReak, DateTime data)
        {
            PhoneId = phoneId;
            PhoneName = phoneName;
            PhoneClass = phoneClass;
            PhonePath = phonePath;
            PhoneState = phoneState;
            PhoneReak = phoneReak;
            Data = data;
        }

        /// <summary>
        /// 相册ID
        /// </summary>
        public string PhoneId { get; set; }

        /// <summary>
        /// 相册名称
        /// </summary>
        public string PhoneName { get; set; }

        /// <summary>
        /// 相册类型
        /// </summary>
        public string PhoneClass { get; set; }

        /// <summary>
        /// 相册路径
        /// </summary>
        public string PhonePath { get; set; }

        /// <summary>
        ///图片状态 
        /// </summary>
        public string PhoneState { get; set; }
        
        /// <summary>
        /// 相册简介
        /// </summary>
        public string PhoneReak { get; set; }

        /// <summary>
        /// 相册入存时间
        /// </summary>
        public DateTime Data { get; set; }
    }
}

 (3)业务逻辑层

1.显示图片

 /// <summary>
        /// 方法:查询显示相册
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public List<Phone> Select_Phone(int index, int size)
        {
            List<Phone> list = new List<Phone>();
            using (MySqlDataReader dr = DBHelper.ExecuteReader("SET @row_number = 0;" +" select * from(select(SELECT(@row_number:= @row_number + 1)) as rowid, s.* from Phone s where PhoneState = '正常' ORDER BY data desc) b" +" where rowid BETWEEN " + size + " * (" + index + " - 1) + 1 and " + size + " * " + index + ""))
            {
                while (dr.Read())
                {
                    list.Add(new Phone
                    {
                        PhoneId = Convert.ToString(dr["PhoneId"]),
                        PhoneName = dr["PhoneName"] as string,
                        PhoneClass = dr["PhoneClass"] as string,
                        PhonePath = dr["PhonePath"] as string,
                        PhoneState = dr["PhoneState"] as string,
                        PhoneReak = dr["PhoneReak"] as string,
                        Data = Convert.ToDateTime(dr["Data"])
                    });
                }
                return list;
            }
        }

2.根据图片类型查询

 /// <summary>
        /// 方法:根据相册类型查询
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="PhoneClass"></param>
        /// <returns></returns>
        public List<Phone> Select_PhoneClass(int index,int size,string PhoneClass)
        {
            List<Phone> list = new List<Phone>();
            using (MySqlDataReader dr = DBHelper.ExecuteReader("SET @row_number = 0;" + " select * from(select(SELECT(@row_number:= @row_number + 1)) as rowid, s.* from Phone s where PhoneClass like '%" + PhoneClass + "%'and PhoneState = '正常' ORDER BY Data desc) b" +" where rowid BETWEEN " + size + " * (" + index + " - 1) + 1 and " + size + " * " + index + ""))
            {
                while (dr.Read())
                {
                    list.Add(new Phone
                    {
                        PhoneId = Convert.ToString(dr["PhoneId"]),
                        PhoneName = dr["PhoneName"] as string,
                        PhoneClass = dr["PhoneClass"] as string,
                        PhonePath = dr["PhonePath"] as string,
                        PhoneState = dr["PhoneState"] as string,
                        PhoneReak = dr["PhoneReak"] as string,
                        Data = Convert.ToDateTime(dr["Data"])
                    });
                }
                return list;
            }
        }

3.根据图片类型名称查询

/// <summary>
        /// 根据相册名称查询
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="PhoneName"></param>
        /// <returns></returns>
        public List<Phone> Select_PhoneName(int index, int size, string PhoneName)
        {
            List<Phone> list = new List<Phone>();
            using (MySqlDataReader dr = DBHelper.ExecuteReader("SET @row_number = 0;" + " select * from(select(SELECT(@row_number:= @row_number + 1)) as rowid, s.* from Phone s where PhoneName like '%" + PhoneName + "%' and PhoneState = '正常' ORDER BY Data desc) b" + " where rowid BETWEEN " + size + " * (" + index + " - 1) + 1 and " + size + " * " + index + ""))
            {
                while (dr.Read())
                {
                    list.Add(new Phone
                    {
                        PhoneId = Convert.ToString(dr["PhoneId"]),
                        PhoneName = dr["PhoneName"] as string,
                        PhoneClass = dr["PhoneClass"] as string,
                        PhonePath = dr["PhonePath"] as string,
                        PhoneState = dr["PhoneState"] as string,
                        PhoneReak = dr["PhoneReak"] as string,
                        Data = Convert.ToDateTime(dr["Data"])
                    });
                }
            }
            return list;
        }

(4)业务逻辑层(BLL)

1.显示图片

/// <summary>
        /// 方法:查询显示相册
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public List<Phone> Select_Phone(int index,int size)
        {
            return cd.Select_Phone(index,size);
        }

2.根据图片类型显示

/// <summary>
        /// 方法:根据相册类型查询
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="PhoneClass"></param>
        /// <returns></returns>
        public List<Phone> Select_PhoneClass(int index, int size, string PhoneClass)
        {
            return cd.Select_PhoneClass(index,size,PhoneClass);
        }

3.根据图片名称显示

 /// <summary>
        /// 方法:根据相册名称查询
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="PhoneName"></param>
        /// <returns></returns>
        public List<Phone> Select_PhoneName(int index,int size,string PhoneName)
        {
            return cd.Select_PhoneName(index,size,PhoneName);
        }

(5)UI

界面

代码

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 Model;
using BLL;
using System.IO;

namespace Phone
{
    public partial class frmMyPhone : Form
    {
        public frmMyPhone()
        {
            InitializeComponent();
        }

        Model.Phone phone = new Model.Phone();
        PhoneBLL cd = new PhoneBLL();
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddPhone frm = new frmAddPhone();
            frm.Show();
        }

        string PagePhone = "相册";
        string PageThme = "相册查询";

        //窗体加载
        private void frmMyPhone_Load(object sender, EventArgs e)
        {
            Select_Phone();
        }

        //显示相册
        public void Select_Phone(int index = 0,int size = 10)
        {
            List<Model.Phone> list = cd.Select_Phone(1,1000);
            if ((list.Count % size) == 0)
            {
                lblsize.Text = list.Count / size + "";
            }
            else
            {
                lblsize.Text = (list.Count / size) + 1 + "";
            }
            lblindex.Text = (++index) + "";
            lvwPhone.Clear();

            List<Model.Phone> lists = cd.Select_Phone(index, size);

            //读取文件
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\");
            FileInfo[] fileInfos = dir.GetFiles();
            string filein = "";

            //获取文件夹下面的文件
            foreach (FileInfo item in fileInfos)
            {
                filein += item.Name;
            }

            for (int i = 0; i < lists.Count; i++)
            {
                if (lists[i].PhonePath != null)
                {
                    if (filein.Contains(lists[i].PhonePath))
                    {
                        imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\" + lists[i].PhonePath));
                    }
                    else
                    {
                        imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\timg.jpg"));
                    }
                }
                else
                {
                    imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\timg.jpg"));
                }
                ListViewItem items = new ListViewItem();
                items.ImageIndex = i;
                items.Text = "标题:" + lists[i].PhoneName + "\n" + "简介:" + lists[i].PhoneReak;
                this.lvwPhone.Items.Add(items);
            }
        }

        //相册相册
        private void comClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            Select_PhoneClass();
        }

        /// <summary>
        /// 根据相册类型查找
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        public void Select_PhoneClass(int index = 0, int size = 1)
        {
            if (comClass.Text == "全部类型")
            {
                Select_Phone();
            }
            else
            {
                this.lvwPhone.Items.Clear();
                List<Model.Phone> list = cd.Select_PhoneClass(1, 10, comClass.Text.Trim());
                DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic");
                FileInfo[] fileInfos = dir.GetFiles();
                string filein = "";

                foreach (FileInfo item in fileInfos)
                {
                    filein += item.Name;
                }

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].PhonePath != "")
                    {
                        if (filein.Contains(list[i].PhonePath))
                        {
                            imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\" + list[i].PhonePath));
                        }
                        else
                        {
                            imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\Resources\timg.jpg"));
                        }
                    }
                    else
                    {
                        imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\"));
                    }

                    ListViewItem items = new ListViewItem();
                    items.ImageIndex = i;
                    items.Text = "标题:" + list[i].PhoneName + "\n" + "简介:" + list[i].PhoneReak;
                    lvwPhone.Items.Add(items);
                }
            }
        }

        //相册名称查找
        private void btnPhoneName_Click(object sender, EventArgs e)
        {
            if (txtPhoneName.Text.Trim() == "")
            {
                //文本框是空的,就清除它们显示的集合
                this.lvwPhone.Items.Clear();
            }
            else if (txtPhoneName.Text.Trim() != "")
            {
                this.lvwPhone.Items.Clear();//再次查询的时候,清空上次搜索的作品
                List<Model.Phone> list = cd.Select_PhoneName(1, 10, txtPhoneName.Text.Trim());
                DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic");
                FileInfo[] fileInfos = dir.GetFiles();
                string filein = "";

                foreach (FileInfo item in fileInfos)
                {
                    filein += item.Name;
                }

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].PhonePath != "")
                    {
                        if (filein.Contains(list[i].PhonePath))
                        {
                            imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\" + list[i].PhonePath));
                        }
                        else
                        {
                            imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\Resources\timg.jpg"));
                        }
                    }
                    else
                    {
                        imageList1.Images.Add(Image.FromFile(Application.StartupPath + @"\..\..\..\Phone\bin\Debug\pic\"));
                    }
                    ListViewItem items = new ListViewItem();
                    items.ImageIndex = i;
                    items.Text = "标题:" + list[i].PhoneName + "\n" + "简介:" + list[i].PhoneReak;
                    lvwPhone.Items.Add(items);
                }
            }
        }

        //首页
        private void btnHome_Click(object sender, EventArgs e)
        {
            Select_Phone();
        }

        //上一页
        private void btnUp_Click(object sender, EventArgs e)
        {
            int index = int.Parse(lblindex.Text) - 2;

            if (index < 0)
            {
                index = 0;
            }

            if (PagePhone == "相册")
            {
                Select_Phone(index);
            }
            else if (PageThme == "相册查询")
            {

            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            int index = int.Parse(lblindex.Text);

            if (index >int.Parse(lblsize.Text) - 1)
            {
                index = int.Parse(lblsize.Text) - 1;
            }

            if (PagePhone == "相册")
            {
                Select_Phone(index);
            }
            else if (PageThme == "相册查询")
            {

            }
        }

        //尾页
        private void btnmax_Click(object sender, EventArgs e)
        {
            if (PagePhone == "相册")
            {
                Select_Phone(int.Parse(lblsize.Text) - 1);
            }
            else if (PageThme == "相册查询")
            {

            }
        }
    }
}

基本的代码就是这些了,如果还没有添加图片的功能的话,可以访问一下:https://blog.csdn.net/qqj3066574300/article/details/88925379   而用到的基本控件有(ComboBox,TextBox,Button,ListView,Label,ImageList)

尾言

代码的故事就只有这么多,代码写得不是很好,就起码功能还是可以实现了,希望这个小小的案例能够帮助到有技术困惑的编程爱好者吧!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值