KTV项目(歌星点歌)

今天我们讲述的如果用win from和sql数据库制作动态的歌星点歌


前言

        我们利用win from制作了KTV点歌系统,而我们今天讲述的是KTV点歌系统里(歌星点歌)

利用软件与数据的链接,在模块上进行有限查询,每次查询八个歌星。点击下一页就会更换下八个歌星。其实利用了sql语句来实现


提示:以下是本篇文章正文内容,下面案例可供参考

一、win from是什么?

Windows Form 可做为多层分散式方案 (Multi-Tier Distributed Solution) 中的本机使用者界面。 

WindowsForm控件

基础控件

Microsoft公司提供的控件非常丰富。 .NET中的大多数控件都派生于 System.Windows.Forms.Control类。它可以分成基础选择和容器类控件组、日期与图片控件组、日期与微调控件组和软件系统框架类控件组。基础选择和容器类控件组:如单选按钮、复选按钮、选项卡等。日期与图片控件组:如Timer、图片框和ImageList等。日期与微调控件组:如NumbericUpDown、DateTimePicker等。软件系统框架类控件组:菜单、StatusStrip控件等。

自主开发控件

自己开发的Windows Form控件通常有三种类型:复合控件,扩展控件,自定义控件,X定义控件。复合控件:将现有的各种控件组合起来,形成一个新的控件,将控件的功能集中起来。扩展控件:在现有控件的基础上新派生出的控件,为原有控件添加新功能或者修改原有控件的功能。自定义控件:直接从System.Windows.Forms.Control类派生出来。Control类提供控件所需要的所有基本功能,包括键盘和鼠标的事件处理。自定义控件是最灵活最强大的方法,但是对开发者的要求也比较高,必须为Control类的OnPaint事件写代码,也可以重写Control类的WndProc方法,处理更底层的Windows消息,所以必须了解GDI+和Windows API。由于自定义控件的复杂性,也可以不必开发,而是从受信任的来源下载控件,并通过添加引用来导入自定义控件。支持Windows Form的控件有:ComponentOne Studio for WinForms、Spread for WinForms、 MultiRow For WinForms等。

二、使用步骤

1.建立歌星面板

        我们在这里采用了点击事件,在点击每个窗口的时候都会触发点击事件。然后获取当前组件lable的text设为变量,用String类型来接受。利用sql语句查询到歌手的歌曲,并且传输给下一个面板作为查询的条件

代码如下(示例):

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace gexing
{
    /// <summary>
    /// 此类维护数据库连接字符串,和 Connection 对象
    /// </summary>
  public class DBHelper
    {
        // 数据库连接字符串
      private  const string connString = @"Data Source=.;Initial Catalog=myktv;User ID=sa;Pwd=gy060321";

        // 数据库连接 Connection 对象
      private static SqlConnection connection;

        /// <summary>
        /// Connection对象
        /// </summary>
        public  static  SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection(connString);
                }
                return connection;
            }            
        }

        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public static void OpenConnection()
        {
            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }
            else if (Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
                Connection.Open();
            }
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public static void CloseConnection()
        {
            if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
            }
        }
    }
}



using gexing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace T3
{
    public partial class PYDG : Form
    {
        public string xm;
        public PYDG()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        //窗体加载
        private void PYDG_Load(object sender, EventArgs e)
        {
            //查询前五条歌曲
            QuerySongList(1);
            //总条数
            int count = QuerySongCount();
            //页数 20, 一页5条   21     %  求余 , /  除法
            int page = count % 5 == 0 ?  count / 5 :  count / 5 + 1;
            this.label8.Text = page.ToString();//赋值
        }
        //查询总条数
        public int QuerySongCount()
        {
            DBHelper.OpenConnection();
           
            string sql="select count(*) from song_info where singer_id=20";
            SqlCommand command = new SqlCommand(sql,DBHelper.Connection);
            int  count = (int)command.ExecuteScalar();
            DBHelper.CloseConnection();
            return count;
        }
        //查询5条歌曲
        public void QuerySongList( int  page)
        {
            DataSet ds = new DataSet(); //临时仓库,数据集   
            string sql = "select  top 5 * from song_info where singer_id = "+Convert.ToInt32(xm);
             if(page>1)
            {
                int start = (page - 1) * 5;
             }   
            
            SqlDataAdapter adapter = 
                new SqlDataAdapter(sql, DBHelper.Connection);
            //放入结果集
            adapter.Fill(ds, "song");
            //获取dataset临时仓库的值
            DataTable dt = ds.Tables["song"];
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                string name = row["song_name"].ToString();
                string url = row["song_url"].ToString();
                //一行 的panel
                Panel  p = this.panel1.Controls[i] as Panel;
                //找到一行的panel中的第一个小控件label
                Label  l = p.Controls[1] as Label;
                l.Text = name; //歌曲名称赋值
                l.Tag = url;//Tag : 存放数据
                i++;
            }
        }
        //下一页
        private void button8_Click(object sender, EventArgs e)
        {
            //第几页
           string p=  this.label6.Text;
           int page = Convert.ToInt32(p) + 1;
           this.label6.Text = page.ToString();//改变label
           //查询page页的内容 从第几条开始查(page-1)*5
         //查询列表
           QuerySongList(page);

        }
        //点歌
        private void button2_Click(object sender, EventArgs e)
        {
           Button btn =  sender as Button; //当前按钮
            //歌曲名称
           string name = btn.Parent.Controls[1].Text;
           string url = btn.Parent.Controls[1].Tag.ToString();
           //存入已点,播放列表  数组 ,集合(List)
           //Song s = new Song();
           //s.SongName = name;
            //准备mp4文件
           //s.SongUrl = "/song/"+url;
           //YiDian.AddSong(s); //播放列表,已点

        }

        private void panel7_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

2.建立歌星点歌的歌曲模版

        我跟根据上一个面板获取的text歌手的名字为sql语句约束条件,在歌曲面板直接进行数据库查询,然后展现在我们在组件上

代码如下(示例):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using T3;

namespace gexing
{
    public partial class dalunan : Form
    {
        public void QuerySongList(int page)
        {
            DataSet ds = new DataSet(); //临时仓库,数据集
            string sql = "select top 8 * from singer_info";
            if (page > 1)
            {
                int start = (page - 1) * 8;
                sql += " where singer_id not in (select top " + start + " singer_id from singer_info)";

            }
                //sql += "and singer_gender='男'";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, DBHelper.Connection);
            //放入结果集
            adapter.Fill(ds, "sing");
            //获取dataset临时仓库的值
            DataTable dt = ds.Tables["sing"];
            int i = 7;
            foreach (DataRow row in dt.Rows)
            {
                string name = row["singer_name"].ToString();
                string url = row["singer_photo_url"].ToString();
                //一行 的panel
                Panel p = this.panel1.Controls[i] as Panel;
                PictureBox box = p.Controls[0] as PictureBox;
                box.Tag = row["singer_id"];
                //找到一行的panel中的第一个小控件label
                Label l = p.Controls[1] as Label;
                l.Text = name; //歌曲名称赋值
                l.Tag = url;//tag:存放路径
                i--;
            }
        }
        public int QuerySongCount()
        {
            DBHelper.OpenConnection();
            string sql = "select count(*) from singer_info";
            SqlCommand command = new SqlCommand(sql, DBHelper.Connection);
            int count = (int)command.ExecuteScalar();
            return count;
        }
        public dalunan()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //第几页
            string p = this.label9.Text;
            int page = Convert.ToInt32(p) + 1;
            this.label9.Text = page.ToString();//改变lable
            //查询page页的内容1,1-5
            //2,6-10 3,11-15
            QuerySongList(page);
        }

        private void dalunan_Load(object sender, EventArgs e)
        {
            //查询前8条歌曲
            QuerySongList(1);
            //总条数
            int count = QuerySongCount();
            //页数,一页5条
            int page = count % 8 == 0 ? count / 8 : count / 8 + 1;
            this.label11.Text = page.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //第几页
            string p = this.label9.Text;
            int page = Convert.ToInt32(p) - 1;
            this.label9.Text = page.ToString();//改变lable
            //查询page页的内容1,1-5
            //2,6-10 3,11-15
            QuerySongList(page);
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void label1_Click(object sender, EventArgs e)
        {
            PYDG p = new PYDG(); //form显示在panel3中
            this.panel2.Controls.Clear();
            this.panel2.Visible = true; //显示
            p.TopLevel = false; //设置form不是顶级
            p.Dock = DockStyle.Fill;//填充
            this.panel2.Controls.Add(p); //把窗体f放入panel3 
            p.Show();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            PictureBox box = sender as PictureBox;
             PYDG p = new PYDG(); //form显示在panel3中
             p.xm = box.Tag.ToString();//id
             p.Show();
        }
    }
}

该处使用数据库请求的数据。


总结

        win from简便的方法,俗称拖拉拽。进行完成系统性的操作。数据库和win from链接,使数据动态性,随时更新和变换。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没白头发的扶苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值