项目名称:KTV点歌系统
--指导老师原玉明
经过一周多的时间,终于完成的我的这个KTV项目,说实话,做这个项目我收获了不少,难点也非常多,不过经过重重磨难,总算是过来了。不简单呀!
这个项目主要分为前后台。
前台主要实现的功能是:歌星点歌,拼音点歌,字数点歌,
类型选择,金曲排行,一些切歌,重唱,已点歌曲列表,主要是这些功能。
后台主要实现的功能是:增加歌手,增加歌曲,修改歌曲路径,修改歌手图片路径。
首先先看前台:
前台核心类:
SqlHelper类(负责连接数据库):
//连接字符串 public static string str = "Data Source=HYJ-PC;Initial Catalog=MyKTV;User ID=sa;pwd=123";
KtvHelper类(保存歌曲的目录;保存歌手图片的目录)
//保存歌曲的目录 public static string songURL = ""; //保存歌手图片的目录 public static string singer_photoURL = ""; FrmByValue类(窗体对象之间的传值) public static frmsonglist frmsl; public static FrmMain frM; public static SelectedSong ss;
Song类(歌曲的信息及实现歌曲状态的一个切换(已播放;未播放;重播;切歌)):
//定义一个枚举:歌曲播放状态 public enum SongPlayState { unplayed, played, newplayed, cut } public class Song { public string SongName { get { return songName; } set { songName = value; } } public string SongURL { get { return songURL; } set { songURL = value; } } internal SongPlayState Playseat { get { return playseat; } set { playseat = value; } } private string songName;//歌曲名称字段 private string songURL;//歌曲存放路径字段 //让歌曲的播放状态默认为未播放; private SongPlayState playseat = SongPlayState.unplayed; //把歌曲状态改为已播放 public void SetSongPlayed() { this.playseat = SongPlayState.played; } //把歌曲状态改为重唱 public void SetnewSong() { this.playseat = SongPlayState.newplayed; } //把歌曲状态改为一切歌 public void Setcut() { this.playseat = SongPlayState.cut; }
PlayList类(播放歌曲的核心内容,功能(实现播放,切歌,重唱)):
public class PlayList { //定义一个数组,默认可以储存50首歌 public static Song[] songList=new Song[50];//歌曲播放列表数组 public static int songIndex;//当前播放的歌曲在数组的索引 #region 添加播放歌曲 public static bool AddSong(Song song) { bool happy = false; for (int i = 0; i < songList.Length; i++) { if (songList[i] == null) { songList[i] = song; happy = true; break; } } return happy; } #endregion #region 当前播放的歌曲名称 public static string PlayingSongName() { string songName = ""; // 歌曲名称 if(songList.Length>songIndex) { if (songList[songIndex] != null) { songName = songList[songIndex].SongName; } } return songName; } #endregion #region 获取当前播放的歌曲 public static Song GetPlayingSong() { if(songIndex<songList.Length) { if (songList[songIndex] != null) { return songList[songIndex]; } else { return null; } } return null; } #endregion #region 下一首要播放的歌曲名称 public static string NextSongName() { string songName = ""; // 歌曲名称 if(songIndex<songList.Length) { if (songList.Length > songIndex + 1) { if (songList[songIndex + 1] != null) { songName = songList[songIndex + 1].SongName; } } } return songName; } #endregion #region 切歌: public static void CutSong() { //代表切歌的位置 int i = songIndex; if (songList[i]!=null) { songList[i].Setcut(); } while (songList[i] != null) { songList[i] = songList[i + 1]; i++; // 如果到达数组最后一个元素,就将最后一个元素指向空 if (i == songList.Length) { songList[i] = null; } } if (songList[0] != null) { songList[0].Playseat = SongPlayState.played; } } #endregion #region 重放当前歌曲 public static void PlayAgain() { if (songList[songIndex] != null) { songList[songIndex].SetnewSong(); } } #endregion #region 播放下一首 public static void MoveOn() { if(songIndex<songList.Length) { if (songList[songIndex] != null && songList[songIndex].Playseat == SongPlayState.newplayed) { songList[songIndex].SetSongPlayed(); } else { songIndex++; } } } #endregion }
实现窗体拖动的代码(找到对应事件双击):
#region 让窗体实现拖动 public Point mouseOffset; //记录鼠标指针的坐标 public bool isMouseDown = false; //记录鼠标按键是否按下 private void FrmMain_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void FrmMain_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X + 5, mouseOffset.Y + 30); Location = mousePos; } } private void FrmMain_MouseUp(object sender, MouseEventArgs e) { // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } #endregion
主界面:(FrmMain)
//当前播放的歌曲 public Song songname; #region 让窗体实现拖动 public Point mouseOffset; //记录鼠标指针的坐标 public bool isMouseDown = false; //记录鼠标按键是否按下 private void FrmMain_MouseDown(object sender, MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void FrmMain_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X + 5, mouseOffset.Y + 30); Location = mousePos; } } private void FrmMain_MouseUp(object sender, MouseEventArgs e) { // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } #endregion // 点击退出按钮触发的事件 private void btnexit_Click(object sender, EventArgs e) { Application.Exit(); } // Login事件 private void FrmMain_Load(object sender, EventArgs e) { //把整个窗体对象赋给FrmByValue的静态frM对象 FrmByValue.frM = this; //运行窗体时启动定时器 this.tilist_played.Start(); //调用查询返回路径的方法,传入SQL语句 // 歌曲路径 string sql = "select resource_path from Resource_path where resource_id=1"; KtvHelper.songURL = song_path(sql); // 歌手图片路径 string sql1 = "select resource_path from Resource_path where resource_id=2"; KtvHelper.singer_photoURL = song_path(sql1); } /// <summary> /// 查询返回路径的方法 /// </summary> /// <param name="sql">传入sql语句</param> /// <returns>根据传入的sql语句返回不同的路径(1.resource_id=1歌曲路径:resource_id=2歌手图片路径)</returns> private string song_path(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); string path = ""; try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { path = dr["resource_path"].ToString(); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } return path; } //点击显示播放触发的事件 private void btnshow_Click(object sender, EventArgs e) { if (this.Width == 567) { this.Width = this.Width + 208; btnshow.Text = "隐 藏 播 放"; } else if (this.Width > 567) { this.Width = 567; btnshow.Text = "显 示 播 放"; } } //已点歌曲窗体 private void btnopen_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); this.Hide(); frm.Show(); } // 点击拼音点歌触发的事件 private void btnspell_Click(object sender, EventArgs e) { frmbySongname frm = new frmbySongname(); this.Hide(); frm.Show(); } //点击字数点歌触发的事件 private void btnnum_Click(object sender, EventArgs e) { frmbyziSong frm = new frmbyziSong(); this.Hide(); frm.Show(); } //点击类型点歌触发的事件 private void btntype_Click(object sender, EventArgs e) { frmbyTypesong frm = new frmbyTypesong(); this.Hide(); frm.Show(); } //定时扫描歌曲列表,显示当前播放歌曲的名称 public void ShowPlayingSongName() { txtopen.Text = PlayList.PlayingSongName(); txtnext.Text = PlayList.NextSongName(); } //播放歌曲的方法 public void PlaySong() { // 获取当前要播放的歌曲 this.songname = PlayList.GetPlayingSong(); if (songname != null) { #region 播放时显示歌曲图片 string name = songname.SongName; string sql = "select singer_id from song_info where song_name =‘" + name + "‘ "; SqlConnection con = new SqlConnection(SqlHelper.str); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); int singerid = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); string sql1 = "select singer_photo from singer_info where singer_id=" + singerid + ""; SqlConnection con1 = new SqlConnection(SqlHelper.str); con1.Open(); SqlCommand cmd1 = new SqlCommand(sql1, con1); string singer_photo = cmd1.ExecuteScalar().ToString(); con1.Close(); plist.Image = Image.FromFile(KtvHelper.singer_photoURL + singer_photo); #endregion // 将当前歌曲播放状态设为已播放 this.songname.SetSongPlayed(); // 得到当前播放歌曲的路径 Playerlist.URL = KtvHelper.songURL + songname.SongURL; } } //计时器控件的Tick事件 private void tilist_played_Tick(object sender, EventArgs e) { ShowPlayingSongName(); if(this.songname==null) { this.PlaySong(); } if (this.Playerlist.playState == WMPLib.WMPPlayState.wmppsStopped) { this.songname = null; PlayList.MoveOn(); } if(this.songname!=null&&this.songname.Playseat==SongPlayState.cut) { this.Playerlist.URL = ""; this.songname = null; } if (this.songname != null && this.songname.Playseat == SongPlayState.newplayed) { this.PlaySong(); } } //点击切歌触发的事件 private void btnnextsong_Click(object sender, EventArgs e) { if (this.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!你还没有添加歌曲"); } } //点击重唱触发的事件 private void btnnew_Click(object sender, EventArgs e) { if (this.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!你还没有添加歌曲"); } } //点击金曲排行触发的时间 private void btnorderby_Click(object sender, EventArgs e) { frmsonglist frm = new frmsonglist(); frm.ph = 1; this.Hide(); frm.Show(); } //歌星点歌 private void btnswan_Click_1(object sender, EventArgs e) { Frmbyswangetsong frm = new Frmbyswangetsong(); frm.Show(); this.Hide(); } //当鼠标进入控件可见状态是触发的事件 private void btnswan_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnswan,"歌星点歌"); } private void btnspell_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnspell, "拼音点歌"); } private void btnnum_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnum, "字数点歌"); } private void btntype_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btntype, "类型选择"); } private void btnorderby_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnorderby, "金曲排行"); } private void btnnew_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnew, "重唱"); } private void btnnextsong_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnextsong, "切歌"); } private void btnopen_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnopen, "已点"); } private void btnfw_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnfw, "服务"); } private void btnexit_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnexit, "退出"); } private void btnfw_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); }
歌星点歌(Frmbyswangetsong):
//图片索引 public int index=0; public int index_singer = 0; //Load事件 private void Frmbyswangetsong_Load(object sender, EventArgs e) { //在显示当前窗体时,让第二个lvlisttwo和第三个lvlistthere控件不显示 lvlisttwo.Visible = false; lvlistthere.Visible = false; #region 给第一个listview绑定数据 //给listview1绑定数据和图片 ListViewItem lv = new ListViewItem("组合", 0); lv.Tag = "组合"; ListViewItem lv1 = new ListViewItem("女歌手", 1); lv1.Tag = "女"; ListViewItem lv2 = new ListViewItem("男歌手", 2); lv2.Tag = "男"; lvlistone.Items.Add(lv); lvlistone.Items.Add(lv1); lvlistone.Items.Add(lv2); #endregion } //点击lvlistone中项时触发的事件 private void lvlistone_Click(object sender, EventArgs e) { //如果没有选中lvlistone控件中的任何一项是不会显示第二个窗体 //让第二个出现的lvlisttwo和lvlistone显示在同一个位置 lvlisttwo.Location = lvlistone.Location; //如果选中一项,就把第二个llvlisttwo显示出来 //让第二个lvlisttwo显示 lvlisttwo.Visible = true; if (lvlistone.SelectedItems[0] != null) { //给第二个listview控件动态绑定数据的方法 lvlisttwoadd(); } } //给第二个listview控件动态绑定数据的方法lvlisttwoadd(); private void lvlisttwoadd() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from singer_type"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { string singertype_group = dr["singertype_name"].ToString(); int id = Convert.ToInt32(dr["singertype_id"]); ListViewItem lv = new ListViewItem(); lv.ImageIndex = index++; lv.Text = singertype_group; lv.Tag= id; // MessageBox.Show(lvlisttwo.Tag.ToString()); lvlisttwo.Items.Add(lv); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } // 03点击lvlisttwo中项时触发的事件 private void lvlisttwo_Click(object sender, EventArgs e) { lvlistthere.Items.Clear(); //如果没有选中lvlisttwo控件中的任何一项是不会显示第三个窗体 //如果选中一项,就把第二个llvlistthere显示出来 if (lvlisttwo.SelectedItems[0] != null) { lvlisttwo.Visible = false; //让第三个出现的lvlistthere和lvlistone显示在同一个位置 lvlistthere.Location = lvlistone.Location; //让第三个lvlistthere显示 lvlistthere.Visible = true; //获取用户点击第一个listview是男歌手还是女歌手或者是组合 string singer_group = lvlistone.SelectedItems[0].Tag.ToString(); //获取用户点击点击第二个listview是哪个地区的id int cityid = Convert.ToInt32(lvlisttwo.SelectedItems[0].Tag); SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select singer_name,singer_photo from singer_info where singer_sex =‘" + singer_group + "‘ and singertype_id="+cityid+" "; SqlCommand cmd = new SqlCommand(sql,con); try { con.Open(); SqlDataReader dr= cmd.ExecuteReader(); if(dr!=null) { if(dr.HasRows) { while (dr.Read()) { //获取到歌手姓名和歌手图片路径 string singer_name = dr["singer_name"].ToString(); string singer_photo_url = dr["singer_photo"].ToString(); //并接路径 string URL = KtvHelper.singer_photoURL + singer_photo_url; imgsingerinfo .Images.Add(Image.FromFile(URL)); ListViewItem lv = new ListViewItem(); lv.Text = singer_name; lv.ImageIndex =index_singer++; lvlistthere.Items.Add(lv); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } } //点击按钮主界面触发的事件 private void btnmain_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } // 点击返回触发的事件 private void btnreturn_Click(object sender, EventArgs e) { if (lvlistthere.Visible == true) { lvlistthere.Visible = false; lvlisttwo.Visible = true; } else if (lvlisttwo.Visible == true) { lvlisttwo.Visible = false; lvlistone.Visible = true; } else { FrmByValue.frM.Show(); this.Close(); } } //点击歌手图片触发的事件 private void lvlistthere_Click(object sender, EventArgs e) { frmsonglist frm = new frmsonglist(); //通过窗体传值把歌手姓名赋给frmsonglist窗体的singer_name变量 frm.singer_name= lvlistthere.SelectedItems[0].Text; frm.Show(); this.Close(); } //点击已点触发的事件 private void btnopen_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); frm.Show(); this.Close(); } //点击切歌触发的事件 private void btnnextsong_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击重唱触发的事件 private void btnnew_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } private void labllist_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(labllist, "歌星点歌"); } private void btnmain_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnmain, "主界面"); } private void btnnew_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnew, "重唱"); } private void btnnextsong_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnextsong, "切歌"); } private void btnopen_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnopen, "已点"); } private void btnfw_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnfw, "服务"); } private void btnreturn_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnreturn, "返回"); } private void btnfw_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); }
拼音点歌(frmbySongname):
//login事件 private void frmbySongname_Load(object sender, EventArgs e) { string RowFilter =""; dgvlistgetzhi( RowFilter); } private void dgvlistgetzhi(string RowFilter) { //取消英文列自动生成 dgvlist.AutoGenerateColumns = false; SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select song_id,song_name,singer_name,song_url,song_ab from song_info,singer_info where song_info.singer_id=singer_info.singer_id"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); try { da.Fill(ds, "info"); DataView dv = new DataView(ds.Tables["info"]); dv.RowFilter = RowFilter; dgvlist.DataSource = dv; } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //点击按钮查询触发的事件 private void btnselect_Click(object sender, EventArgs e) { string RowFilter = " song_ab like ‘%" + txtlist.Text + "%‘ or song_name like ‘%" + txtlist.Text + "%‘ "; dgvlistgetzhi(RowFilter); } ///点击单元格触发的事件 private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "") { Song so = new Song(); string song_name = dgvlist.SelectedRows[0].Cells["song_name"].Value.ToString(); string song_url = dgvlist.SelectedRows[0].Cells["song_url"].Value.ToString(); so.SongName = song_name; so.SongURL = song_url; bool result = PlayList.AddSong(so); if (result == true) { MessageBox.Show("添加成功!"); int songid = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_Id"].Value); SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "update song_info set song_play_count=song_play_count+1 where song_id=‘" + songid + "‘"; try { con.Open(); SqlCommand cmd = new SqlCommand(sql, con); int count = cmd.ExecuteNonQuery(); } catch (Exception) { MessageBox.Show("失败"); } finally { con.Close(); } } else { MessageBox.Show("添加失败!"); } } else { MessageBox.Show("请选择一首歌"); } } //点击已点触发的事件 private void button1_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); frm.Show(); this.Close(); } //点击切歌触发的事件 private void button2_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击重唱触发的事件 private void button5_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } private void button3_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } private void label2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(label2, "拼音点歌"); } private void button3_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button3, "主界面"); } private void button5_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button5, "重唱"); } private void button2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button2, "切歌"); } private void button1_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button1, "已点"); } private void button6_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button6, "服务"); } private void button6_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务...."); } private void btnselect_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnselect, "查询"); }
字数点歌(frmbyziSong):
//login事件 private void frmbyziSong_Load(object sender, EventArgs e) { // 将字数列表添加到窗体中 for (int i = 0; i < 12; i++) { // 循环生成字数项添加到窗体中 ListViewItem item = new ListViewItem(); item.Text = (i + 1) + "个字"; item.Tag = i + 1; lvlist.Items.Add(item); } } //单击listview组件触发的事件 private void lvlist_Click(object sender, EventArgs e) { //获取每一个item.Tag对应的值 int count= Convert.ToInt32(lvlist.SelectedItems[0].Tag); frmsonglist frm = new frmsonglist(); frm.count = count; frm.Show(); this.Close(); } //点击主界面触发的事件 private void button3_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } //点击已点触发的事件 private void button1_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); frm.Show(); } private void button2_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!还没有歌曲"); } } private void button5_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } private void label2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(label2, "字数点歌"); } private void button3_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button3, "主界面"); } private void button5_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button5, "重唱"); } private void button2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button2, "切歌"); } private void button1_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button1, "已点"); } private void button6_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button6, "服务"); } private void button6_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); }
类型选择(frmbyTypesong):
//定义一个变量index,表示imagelist int index = 0; // login事件 private void frmbyTypesong_Load(object sender, EventArgs e) { //给listview控件绑定值 lvlistgetzhi(); } // 给listview控件绑定值 private void lvlistgetzhi() { // 读取歌曲类别 SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from song_type"; SqlCommand cmd = new SqlCommand(sql, con); try { // 查询数据库 con.Open(); SqlDataReader dr = cmd.ExecuteReader(); // 循环将类别读取出来添加到ListView中 lvlist.Items.Clear(); if(dr!=null) { if(dr.HasRows) { while (dr.Read()) { ListViewItem item = new ListViewItem(); item.Text = Convert.ToString(dr["songtype_name"]); item.Tag = Convert.ToInt32(dr["songtype_id"]); item.ImageIndex = index++; lvlist.Items.Add(item); } } } dr.Close(); } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //点击listview控件中组件触发的事件 private void lvlist_Click(object sender, EventArgs e) { //获取选中的歌曲隐藏的Tag值 int Tagid = Convert.ToInt32(lvlist.SelectedItems[0].Tag); //new出歌曲列表窗体对象 frmsonglist frm = new frmsonglist(); //把获取的歌曲隐藏值Tag赋给frmsonglist窗体的公有变量Tagid frm.Tagid = Tagid; frm.Show(); this.Close(); } //点击已点触发的事件 private void button7_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); frm.Show(); this.Close(); } //点击重唱触发的事件 private void button11_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击切歌触发的事件 private void button8_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击主界面触发的事件 private void button9_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } private void label2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(label2, "分类点歌"); } private void button9_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button9, "主界面"); } private void button11_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button11, "重唱"); } private void button8_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button8, "切歌"); } private void button7_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button7, "已点"); } private void button12_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(button12, "服务"); } private void button12_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); }
金曲排行(frmsonglist公用窗体):
//接收金曲排行传过来的值 public int ph; public int Tagid; //接收窗体传过来的歌手姓名 public string singer_name; public string name = ""; //并接的歌曲完整路径 public string URL1; //获取歌曲名 //public string singer_name = ""; //获取歌曲演唱者 public string song_name =""; public int index = 0; public int count = 0; //Login事件 private void frmsonglist_Load(object sender, EventArgs e) { //FrmByValue.frmsl = this; //给listview控件绑定值 if (Tagid != 0) { string sql = "select song_name,singer_name,song_url,song_id from singer_info,song_info where singer_info.singer_id=song_info.singer_id and songtype_id=" + Tagid + ""; lvlistgetzhi(sql); } else if(count!=0) { string sql = "select song_name,singer_name,song_url,song_id from singer_info,song_info where singer_info.singer_id=song_info.singer_id and song_word_count="+count+""; lvlistgetzhi(sql); } else if(ph!=0) { string sql = "select song_name,singer_name,song_url,song_id from singer_info,song_info where singer_info.singer_id=song_info.singer_id order by song_play_count desc"; lvlistgetzhi(sql); } else { string sql = "select song_id,song_name,singer_name,song_url from singer_info,song_info where singer_info.singer_id=song_info.singer_id and singer_name=‘" + singer_name + "‘"; lvlistgetzhi(sql); } } //给listview控件绑定值的方法lvlistgetzhi(string sql)传入SQL语句 public void lvlistgetzhi(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); try { da.Fill(ds, "info"); dgvlist.DataSource = ds.Tables["info"]; } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //点击主界面触发的事件 private void btnmain_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } //点击dgvlsit单元格触发的事件 private void dgvlist_CellClick_1(object sender, DataGridViewCellEventArgs e) { //点选中行不为空时把歌曲信息添加到Song类的歌曲名称和歌曲路径属性中 if (dgvlist.SelectedRows[0].Cells[0].Value.ToString()!= "") { //获取歌曲路径 string songpath = dgvlist.SelectedRows[0].Cells["song_url"].Value.ToString(); //获取歌曲名 string song_name = dgvlist.SelectedRows[0].Cells["songname"].Value.ToString(); Song so = new Song(); so.SongName = song_name; so.SongURL = songpath; bool result = PlayList.AddSong(so); if (result == true) { MessageBox.Show("添加成功!"); } else { MessageBox.Show("添加失败!"); } // 更新数据库,将选中的歌曲点播次数加1 update_count(); } else { MessageBox.Show("请选择一首歌"); } } //更新数据库,将选中的歌曲点播次数加1的方法 private void update_count() { int songid = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_Id"].Value); SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "update song_info set song_play_count=song_play_count+1 where song_id=‘" + songid + "‘"; try { con.Open(); SqlCommand cmd = new SqlCommand(sql, con); int count = cmd.ExecuteNonQuery(); } catch (Exception) { MessageBox.Show("失败"); } finally { con.Close(); } } //点击已点触发的事件 private void btnopen_Click(object sender, EventArgs e) { SelectedSong frm = new SelectedSong(); this.Close(); frm.Show(); } //点击切歌触发的事件 private void btnnextsong_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); } else { MessageBox.Show("亲!还没有歌曲"); } } // 点击重唱触发的事件 private void btnnew_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void label2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(label2,"歌曲列表"); } private void btnmain_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnmain, "主界面"); } private void btnnew_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnew, "重唱"); } private void btnnextsong_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnextsong, "切歌"); } private void btnopen_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnopen, "已点"); } private void btnfw_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnfw, "服务"); } private void btnfw_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); } }
已点列表(SelectedSong):
//login事件 private void SelectedSong_Load(object sender, EventArgs e) { FrmByValue.ss = this; //给一点列表绑定值 getselectsongzhi(); } public void getselectsongzhi() { lvlist.Items.Clear(); // 清空原列表 //定义一个数组长度为50的对象数组 Song[] so = new Song[50]; so = PlayList.songList; //用foreacher循环出对象数组里的歌曲信息 foreach (Song item in so) { if (item != null) { //遍历出歌曲名 string song_name = item.SongName; //遍历出歌曲路径 string song_url = item.SongURL; //遍历出播放状态 //对于这行代码的理解 string type = item.Playseat == SongPlayState.unplayed ? "未播放" : "已播放"; //<<item.Playseat>>如果他保存的是 SongPlayState.unplayed 那么(tem.Playseat == SongPlayState.unplayed)==true,就会把未播放的值赋给type变量 //<<item.Playseat>>如果他保存的是 不是SongPlayState.unplayed 是枚举类型的其他值那么(tem.Playseat == SongPlayState.unplayed)==false,就会把已播放的值赋给type变量 string type = item.Playseat == SongPlayState.unplayed ? "未播放" : "已播放"; //给lvlist绑定值 ListViewItem lv = new ListViewItem(song_name); lv.SubItems.Add(type); lvlist.Items.Add(lv); } } } //点击切歌触发的事件 private void btnnextsong_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.CutSong(); getselectsongzhi(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击重唱触发的事件 private void btnnew_Click(object sender, EventArgs e) { if (FrmByValue.frM.songname != null) { PlayList.PlayAgain(); } else { MessageBox.Show("亲!还没有歌曲"); } } //点击主界面触发的事件 private void btnmain_Click(object sender, EventArgs e) { FrmByValue.frM.Show(); this.Close(); } private void label2_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(label2, "已点列表"); } private void btnmain_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnmain, "主界面"); } private void btnnew_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnew, "重唱"); } private void btnnextsong_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnnextsong, "切歌"); } private void btnfw_MouseEnter(object sender, EventArgs e) { ToolTip tp = new ToolTip(); tp.ShowAlways = true; tp.SetToolTip(btnfw, "服务"); } private void btnfw_Click(object sender, EventArgs e) { MessageBox.Show("正在呼叫服务..."); }
后台:
核心类:
Hanzitopinyin类(汉字转拼音)
{ "A阿啊锕嗄厑哎哀唉埃挨溾锿鎄啀捱皑凒溰嘊敳皚癌毐昹嗳矮蔼躷噯藹譪霭靄艾伌爱砹硋隘嗌塧嫒愛碍暧瑷僾壒嬡懓薆曖璦鴱皧瞹馤鑀鱫安侒峖桉氨庵谙萻腤鹌蓭誝鞌鞍盦馣鮟盫韽啽雸垵" , //此处有省略 }/// <summary> /// 获得一个字符串的汉语拼音码 /// </summary> /// <param name="strText">字符串</param> /// <returns>汉语拼音码,该字符串只包含大写的英文字母</returns> public static string GetChineseSpell(string strText) { if (strText == null || strText.Length == 0) return strText; System.Text.StringBuilder myStr = new System.Text.StringBuilder(); foreach (char vChar in strText) { // 若是字母则直接输出 if ((vChar >= ‘a‘ && vChar <= ‘z‘) || (vChar >= ‘A‘ && vChar <= ‘Z‘)) myStr.Append(char.ToUpper(vChar)); else if ((int)vChar >= 19968 && (int)vChar <= 40869) { // 若字符Unicode编码在编码范围则 查汉字列表进行转换输出 foreach (string strList in strChineseCharList) { if (strList.IndexOf(vChar) > 0) { myStr.Append(strList[0]); break; } } } } return myStr.ToString(); }// GetChineseSpell
SqlHelper类(负责连接数据库):
//连接字符串 public static string str = "Data Source=HYJ-PC;Initial Catalog=MyKTV;User ID=sa;pwd=123";
KtvHelper类(保存歌曲的目录;保存歌手图片的目录)
//保存歌曲的目录 public static string songURL = ""; //保存歌手图片的目录 public static string singer_photoURL = "";
登录界面(frnAdmin):
private void btnlogin_Click(object sender, EventArgs e) { //登录 Login(); } //登录的方法 public void Login() { //获取用户名 string loginname = txtname.Text; //获取密码 string pwd = txtpassword.Text; SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from admin_info where admin_name=‘" + loginname + "‘ and admin_pwd=‘" + pwd + "‘ "; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { MessageBox.Show("登录成功!"); this.Hide(); frmMain frm = new frmMain(); frm.Show(); } else { MessageBox.Show("登录失败!"); } } catch (Exception) { MessageBox.Show("网路异常!"); } finally { con.Close(); } } private void btnexit_Click(object sender, EventArgs e) { Application.Exit(); }
主界面(frmMain):
private void 编辑歌手信息ToolStripMenuItem_Click(object sender, EventArgs e) { AddSingerInfo frm = new AddSingerInfo(); frm.MdiParent = this; frm.Show(); } private void 歌手查询ToolStripMenuItem_Click(object sender, EventArgs e) { frmsingerselect frm = new frmsingerselect(); frm.MdiParent = this; frm.Show(); } //点击编辑歌曲信息触发的事件 private void 编辑歌曲信息ToolStripMenuItem_Click(object sender, EventArgs e) { AddSongInfo frm = new AddSongInfo(); frm.MdiParent = this; frm.Show(); } private void 歌曲查询ToolStripMenuItem_Click(object sender, EventArgs e) { frmsongselect frm = new frmsongselect(); frm.MdiParent = this; frm.Show(); } private void frmMain_Load(object sender, EventArgs e) { //调用查询返回路径的方法,传入SQL语句 // 歌曲路径 string sql = "select resource_path from Resource_path where resource_id=1"; KTVUtil.songURL = song_path(sql); // 歌手图片路径 string sql1 = "select resource_path from Resource_path where resource_id=2"; KTVUtil.singer_photoURL = song_path(sql1); } /// <summary> /// 查询返回路径的方法 /// </summary> /// <param name="sql">传入sql语句</param> /// <returns>根据传入的sql语句返回不同的路径(1.resource_id=1歌曲路径:resource_id=2歌手图片路径)</returns> private string song_path(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); string path = ""; try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { path = dr["resource_path"].ToString(); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } return path; } //修改歌曲路径 private void 设置歌曲路径ToolStripMenuItem_Click(object sender, EventArgs e) { frmsongurl frm = new frmsongurl(); frm.MdiParent = this; frm.Show(); } private void 修改歌手图片路径ToolStripMenuItem_Click(object sender, EventArgs e) { frmsingerphotourl frm = new frmsingerphotourl(); frm.MdiParent = this; frm.Show(); } //退出 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); }
新增歌手(FrmAddSingerinfo):
//相对路径 public string txtFileName; //绝对路径 public string txtPath; //用来接受窗体传过来的值 //歌手编号 public int singer_id; //点击浏览触发的时间 private void btnll_Click(object sender, EventArgs e) { //Fiter属性的构成:Excel文件|*.xls,Exce是一个可读的自定义字符串;|*.xls筛选器 //试用*表示匹配文件名的字符使用.后缀匹配文件的后缀名,使用;来将需要的后缀分开通过|连接不同的筛选器表示通过用户选择后缀名来进行文件筛选 openFileDialog1.Filter="图片文件|*.jpg;*.png;*.gif;"; //01.openFileDialog1.Filter="图片文件|*.jpg;*.png;*.gif"; //02.openFileDialog1.Filter = "(*.jpg,*.gif,*.png;)|*.jpg;*.gif;*.png;"; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK)//证明用户双击(选中了)一个文件,我就获取路径 { //相对路径 txtFileName = openFileDialog1.SafeFileName; //绝对路径 txtPath = openFileDialog1.FileName; pblist.Image = Image.FromFile(txtPath); } } //点击添加触发的事件 private void btnok_Click(object sender, EventArgs e) { if (infoisnull()) { if (singer_id != 0) { //执行修改歌手信息的方法 Updatesingerinfo(); } else { //添加歌手信息的方法 Addsingerinfo(); } } } //执行修改歌手信息的方法 public void Updatesingerinfo() { //获取添加的歌手姓名 string singer_name = txtname.Text; //获取添加歌手的性别信息 string gender; if (rbboy.Checked == true) { gender = "男"; } else if (rbgerey.Checked == true) { gender = "女"; } else { gender = "组合"; } //获取歌手类型的隐藏值 int stype_id = Convert.ToInt32(cbolist.SelectedValue); //获取歌手描述的信息 string singer_ms = txtms.Text; // SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "update singer_info set singer_name=‘" + singer_name + "‘,singer_sex=‘" + gender + "‘,singertype_id=" + stype_id + ",singer_describe=‘" + singer_ms + "‘,singer_photo=‘" + txtFileName + "‘ where singer_id=" + singer_id + ""; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("修改成功!"); if (txtPath != null) { if (txtPath != KTVUtil.singer_photoURL + txtFileName) { File.Copy(txtPath, KTVUtil.singer_photoURL + txtFileName, true); } } } else { MessageBox.Show("修改失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //load事件 private void AddSingerInfo_Load(object sender, EventArgs e) { //给歌手类型下拉框绑定数据 getcbolistzhi(); if (singer_id != 0) { this.Text = "修改歌手信息"; btnok.Text = "修改"; updatagetzhi(); } else { this.Text = "编辑歌歌手信息"; btnok.Text = "保存"; } } //给歌手类型下拉框绑定数据的方法 public void getcbolistzhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from dbo.singer_type"; SqlDataAdapter da = new SqlDataAdapter(sql,con); DataSet ds = new DataSet(); try { da.Fill(ds,"info"); cbolist.DataSource=ds.Tables["info"]; cbolist.DisplayMember = "singertype_name"; cbolist.ValueMember = "singertype_id"; } catch (Exception) { MessageBox.Show("网路异常!"); } finally { con.Close(); } } //修改时给修改窗体赋值 public void updatagetzhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select singer_photo,singer_name,singer_sex,singertype_name,singer_describe from singer_info,singer_type where singer_info.singertype_id=singer_type.singertype_id and singer_id=" + singer_id + ""; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { //给歌手姓名文本框赋值 txtname.Text = dr["singer_name"].ToString(); //选中性别按钮 if (dr["singer_sex"].ToString().Equals("男")) { rbboy.Checked = true; } else if (dr["singer_sex"].ToString().Equals("女")) { rbgerey.Checked = true; } else { rbbox.Checked = true; } cbolist.Text = dr["singertype_name"].ToString(); txtms.Text = dr["singer_describe"].ToString(); string url= dr["singer_photo"].ToString(); if (url!="") { pblist.Image = Image.FromFile(KTVUtil.singer_photoURL + url); } txtFileName = url; } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //添加歌手信息的方法 public void Addsingerinfo() { if (txtFileName != null) { #region 获取添加的歌手的信息 //获取添加的歌手姓名 string singer_name = txtname.Text; //获取添加歌手的性别信息 string gender; if (rbboy.Checked == true) { gender = "男"; } else if (rbgerey.Checked == true) { gender = "女"; } else { gender = "组合"; } //获取歌手类型的隐藏值 int stype_id = Convert.ToInt32(cbolist.SelectedValue); //获取歌手描述的信息 string singer_ms = txtms.Text; #endregion SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "insert into singer_info values(‘" + singer_name + "‘," + stype_id + ",‘" + gender + "‘,‘" + txtFileName + "‘,‘" + txtms.Text + "‘) "; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("添加成功!"); if (txtPath != null) { if (txtPath != KTVUtil.singer_photoURL + txtFileName) { File.Copy(txtPath, KTVUtil.singer_photoURL + txtFileName, true); } } } else { MessageBox.Show("添加失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } else { MessageBox.Show("请选择歌手图片路径"); } } /// 验证用户界面输入的信息是否为空 private bool infoisnull() { bool happy = false; if (txtname.Text.Trim() == "") { MessageBox.Show("请输入歌手姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtname.Focus(); } else if (cbolist.SelectedValue == null) { MessageBox.Show("请选择歌手类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cbolist.Focus(); } else if(rbboy.Checked==false&&rbbox.Checked==false&&rbgerey.Checked==false) { MessageBox.Show("请选择歌手性别", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { happy = true; } return happy; } //关闭 private void btnexit_Click(object sender, EventArgs e) { this.Close(); }
歌手信息查询(frmsingerselect)
public AddSongInfo add; //Load事件 private void frmsingerselect_Load(object sender, EventArgs e) { // 给歌手类型下拉框绑定数据的方法 getcbolistzhi(); if(cbolist.Text=="全部") { txtname.Enabled = false; } //给dgvlist控件绑定数据 string RowFilter = ""; getdgvlistzhi(RowFilter); } //给歌手类型下拉框绑定数据的方法 public void getcbolistzhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from dbo.singer_type"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); try { da.Fill(ds, "info"); cbolist.DataSource = ds.Tables["info"]; cbolist.DisplayMember = "singertype_name"; cbolist.ValueMember = "singertype_id"; DataRow row = ds.Tables["info"].NewRow(); row["singertype_id"] = -1; row["singertype_name"] = "全部"; ds.Tables["info"].Rows.InsertAt(row, 0); cbolist.SelectedIndex = 0; } catch (Exception) { MessageBox.Show("网路异常!"); } finally { con.Close(); } } //给dgvlist控件绑定数据的方法 public void getdgvlistzhi(string RowFilter) { dgvlist.AutoGenerateColumns = false; //歌手姓名,歌手类型,歌手性别,歌手描述 SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select singer_info.singertype_id,singer_name,singer_id,singertype_name,singer_sex,singer_describe from dbo.singer_info,dbo.singer_type where singer_info.singertype_id=singer_type.singertype_id "; SqlDataAdapter da = new SqlDataAdapter(sql,con); DataSet ds = new DataSet(); try { da.Fill(ds,"info"); DataView dv = new DataView(ds.Tables["info"]); dv.RowFilter = RowFilter; dgvlist.DataSource = dv; } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //点击查询触发的事件 private void btnselect_Click(object sender, EventArgs e) { } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "") { DialogResult result = MessageBox.Show("你确定删除该歌手信息吗?(会同时删除歌曲信息)", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { //删除歌手信息的方法 deletesonginfo(); deletesingerinfo(); } } else { MessageBox.Show("请选择一名歌手"); } } //删除歌手对应的歌曲信息的方法 public void deletesonginfo() { //获取选中行的歌手编号 int singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["ID"].Value); string sql = "delete song_info where singer_id="+singer_id+""; SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql,con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } //删除歌手信息的方法 public void deletesingerinfo() { //获取选中行的歌手编号 int singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["ID"].Value); string sql1 = "select song_id from song_info where singer_id="+singer_id+""; SqlConnection con1 = new SqlConnection(SqlHelper.str); SqlCommand cmd1 = new SqlCommand(sql1,con1); con1.Open(); int song_id= Convert.ToInt32(cmd1.ExecuteScalar()); con1.Close(); if (song_id != 0) { MessageBox.Show("请先删除该歌手的歌曲信息"); } else { //并接SQL语句 string sql = "delete dbo.singer_info where singer_id=" + singer_id + " "; SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("删除成功!"); string RowFilter = ""; getdgvlistzhi(RowFilter); } else { MessageBox.Show("删除失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } } //点击单元格触发的事件 private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e) { } //点击修改触发的事件 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "") { //获取选中行的歌手信息 //歌手编号 int singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["ID"].Value); AddSingerInfo frm = new AddSingerInfo(); frm.singer_id = singer_id; frm.Show(); } else { MessageBox.Show("请选择一名歌手"); } } private void dgvlist_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { //获取用户选中行的歌手姓名 string singername = dgvlist.SelectedRows[0].Cells["singer_name"].Value.ToString(); //获取用户选中行的歌手编号 int singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["ID"].Value); //给新增歌曲窗体的歌手文本框赋值 add.txtsingername.Text = singername; add.txtsingername.Tag = singer_id; } private void btnselect_Click_1(object sender, EventArgs e) { if (txtname.Text == "" && cbolist.Text == "全部") { string RowFilter = ""; getdgvlistzhi(RowFilter); } else if (txtname.Text != ""&&cbolist.Text=="全部") { //获取要查询歌手的姓名 string name = txtname.Text; //获取要查询的歌手类型 int type = Convert.ToInt32(cbolist.SelectedValue); string RowFilter = "singer_name like ‘%" + name + "%‘"; getdgvlistzhi(RowFilter); } else { //获取要查询歌手的姓名 string name = txtname.Text; //获取要查询的歌手类型 int type = Convert.ToInt32(cbolist.SelectedValue); string RowFilter = "singer_name like ‘%" + name + "%‘ and singertype_id=" + type + ""; getdgvlistzhi(RowFilter); } } private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void cbolist_SelectedIndexChanged(object sender, EventArgs e) { if (cbolist.Text == "全部") { txtname.Enabled = false; } else { txtname.Enabled = true; } }
新增歌曲(AddSongInfo):
public frmsingerselect frmss; public int song_id; public string txtPath; //相对路径 public string txtxdpath; //Load事件 private void AddSongInfo_Load(object sender, EventArgs e) { //给歌曲类型下拉框绑定数据 getcbolistzhi(); if (song_id != 0) { this.Text = "修改歌曲信息"; btnok.Text = "修改"; updategetzhi(); } else { this.Text = "编辑歌曲信息"; btnok.Text = "保存"; } } //给歌曲类型下拉框绑定数据的方法 public void getcbolistzhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from dbo.song_type"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); try { da.Fill(ds, "info"); cbolist.DataSource = ds.Tables["info"]; cbolist.DisplayMember = "songtype_name"; cbolist.ValueMember = "songtype_id"; } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Open(); } } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "歌曲文件|*.mp3;"; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK)//证明用户双击(选中了)一个文件,我就获取路径 { //相对路径 txtxdpath = openFileDialog1.SafeFileName; txturl.Text = txtxdpath; //绝对路径 txtPath = openFileDialog1.FileName; int dot = txtxdpath.LastIndexOf(‘.‘); string fileType = txtxdpath.Substring(dot + 1); if (fileType != "mp3") { MessageBox.Show("文件类型错误!"); } ////给PictureBox的Image属性赋值 //pblist.Image = Image.FromFile(txtPath); } } //点击查询触发的事件 private void button4_Click(object sender, EventArgs e) { frmsingerselect frm = new frmsingerselect(); frm.add = this; frm.ShowDialog(); } //点击关闭触发的事件 private void button3_Click(object sender, EventArgs e) { this.Close(); } //点击保存触发的事件 private void button2_Click(object sender, EventArgs e) { if (isnull()) { if (song_id != 0) { //执行修改的方法 updatesonginfo(); } else { //保存的方法 addinfo(); } } } //执行修改给修改窗体相关信息赋值 public void updategetzhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select song_info.singer_id,song_url,song_name,singer_name,song_ab,song_word_count,song_url,song_play_count,songtype_name from song_info,song_type,singer_info where song_info.songtype_id=song_type.songtype_id and song_info.singer_id=singer_info.singer_id and song_id=" + song_id + ""; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { txtsongname.Text = dr["song_name"].ToString(); txtsingername.Text = dr["singer_name"].ToString(); txtsingername.Tag = Convert.ToInt32(dr["singer_id"]); txtpy.Text = dr["song_ab"].ToString(); cbolist.Text = dr["songtype_name"].ToString(); txtsingername.Text = dr["singer_name"].ToString(); txturl.Text = dr["song_url"].ToString(); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //修改歌曲信息的方法 public void updatesonginfo() { //获取歌曲名称 string song_name = txtsongname.Text; //获取拼音缩写信息 string py = txtpy.Text; //获取歌曲类型对应的隐藏值 int type_id = Convert.ToInt32(cbolist.SelectedValue); //获取歌手姓名对应的编号 int singe_id = Convert.ToInt32(txtsingername.Tag); //获取歌曲文件名 string url = txturl.Text; //获取歌曲名称的长度 int length = song_name.Length; string sql = "update song_info set song_name=‘" + song_name + "‘,song_ab=‘" + py + "‘,song_word_count=" + length + ",songtype_id=" + type_id + ",singer_id=" + singe_id + ",song_url=‘" + url + "‘,song_play_count=default where song_id=" + song_id + ""; SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int result = cmd.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("修改成功!"); if (txtPath != null) { if (txtPath != KTVUtil.songURL + txtxdpath) { File.Copy(txtPath, KTVUtil.songURL + txtxdpath, true); } } else { MessageBox.Show("修改失败!"); } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } //新增歌曲信息的方法 public void addinfo() { //获取歌曲名称 string song_name = txtsongname.Text; //获取拼音缩写信息 string py = txtpy.Text; //获取歌曲类型对应的隐藏值 int type_id = Convert.ToInt32(cbolist.SelectedValue); //获取歌手姓名对应的编号 int singe_id = Convert.ToInt32(txtsingername.Tag); //获取歌曲文件名 string url = txturl.Text; //获取歌曲名称的长度 int length = song_name.Length; //拼接sql语句 string sql = "insert into song_info values(‘" + song_name + "‘,‘" + py + "‘," + length + "," + type_id + "," + singe_id + ",‘" + url + "‘,default)"; SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int result = cmd.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("保存成功!"); if (txtPath != null) { if (txtPath != KTVUtil.songURL + txtxdpath) { File.Copy(txtPath, KTVUtil.songURL + txtxdpath, true); } } } else { MessageBox.Show("保存失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } private void txtsongname_TextChanged(object sender, EventArgs e) { //将第一个文本框中内容转换成py,写入到第二个文本框中 string word = txtsongname.Text; if (word != "") { txtpy.Text = hanzitopinyin.GetChineseSpell(word); } else { txtpy.Text = string.Empty; } } public bool isnull() { bool happy = false; if (txtsongname.Text.Trim() == "") { MessageBox.Show("请输入歌曲名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtsongname.Focus(); } else if (txtpy.Text.Trim() == "") { MessageBox.Show("请输入拼音缩写", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtpy.Focus(); } else if(cbolist.Text==null ) { MessageBox.Show("青选择歌曲类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (txtsingername.Text=="") { MessageBox.Show("请选择歌手", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtsingername.Focus(); } else if (txturl.Text == "") { MessageBox.Show("请选择歌曲路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txturl.Focus(); } else { happy = true; } return happy; }
查询歌曲信息(frmsongselect):
//Load事件 private void frmsongselect_Load(object sender, EventArgs e) { //给歌曲类型下拉框赋值 getsongtypezhi(); if (cbolist.Text == "全部") { txtsongname.Enabled = false; } //给dgvlist绑定数据 string RowFilter = ""; getdgvlistzhi(RowFilter); } //给歌曲类型下拉框赋值的方法 public void getsongtypezhi() { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select * from song_type"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); try { da.Fill(ds, "info"); cbolist.DataSource = ds.Tables["info"]; cbolist.DisplayMember = "songtype_name"; cbolist.ValueMember = "songtype_id"; DataRow row = ds.Tables["info"].NewRow(); row["songtype_id"] = -1; row["songtype_name"] = "全部"; ds.Tables["info"].Rows.InsertAt(row,0); cbolist.SelectedIndex =0; } catch (Exception) { MessageBox.Show("网路异常!"); } finally { con.Close(); } } //给dgvlist绑定数据的方法 public void getdgvlistzhi(string RowFilter) { //取消英文列自动生成 dgvlist.AutoGenerateColumns = false; SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select song_id,song_name,songtype_name,song_play_count,song_info.songtype_id from song_info,song_type where song_info.songtype_id=song_type.songtype_id"; SqlDataAdapter da = new SqlDataAdapter(sql,con); DataSet ds = new DataSet(); try { da.Fill(ds,"info"); DataView dv = new DataView(ds.Tables["info"]); dv.RowFilter = RowFilter; dgvlist.DataSource = dv; } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Open(); } } //点击查询触发的事件 private void btnselect_Click(object sender, EventArgs e) { if (txtsongname.Text == "" && cbolist.Text == "全部") { string RowFilter = ""; getdgvlistzhi(RowFilter); } else if (txtsongname.Text != "" && cbolist.Text == "全部") { //获取歌曲名称 string song_name = txtsongname.Text; //获取歌曲类型对应的隐藏值 int type_id = Convert.ToInt32(cbolist.SelectedValue); string RowFilter = "song_name like ‘%" + song_name + "%‘"; getdgvlistzhi(RowFilter); } else { //获取歌曲名称 string song_name = txtsongname.Text; //获取歌曲类型对应的隐藏值 int type_id = Convert.ToInt32(cbolist.SelectedValue); string RowFilter = "song_name like ‘%" + song_name + "%‘ and songtype_id =" + type_id + ""; getdgvlistzhi(RowFilter); } } //点击dgvlist单元格触发的事件 private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e) { } //删除歌曲信息的方法 public void deletesong() { if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "") { //获取选中行的歌曲编号 int song_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_id"].Value); DialogResult result = MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "delete song_info where song_id=" + song_id + ""; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } } else { MessageBox.Show("请选择一首歌曲"); } } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { //删除歌曲信息的方法 deletesong(); } //修改 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "") { //获取选中行的歌曲编号 int song_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_id"].Value); AddSongInfo frm = new AddSongInfo(); frm.song_id = song_id; frm.Show(); } else { MessageBox.Show("请选择一首歌曲"); } } private void cbolist_SelectedIndexChanged(object sender, EventArgs e) { if (cbolist.Text == "全部") { txtsongname.Enabled = false; } else { txtsongname.Enabled = true; } }
修改歌手信息(共用窗体新增歌手(FrmAddSingerinfo)):
修改歌曲信息(共用窗体新增歌曲(AddSongInfo):):
修改歌手图片路径(Frmsingerphotourl)
private void button2_Click(object sender, EventArgs e) { // 如果新路径为空,提示 if (this.txtnewurl.Text.Trim() == "") { MessageBox.Show("请选择新路径!"); } else { // 用户确认修改 if (MessageBox.Show("确定要修改路径吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Directory.Delete(txtnewurl.Text); Directory.Move(txturl.Text, txtnewurl.Text); string newUrl = txtnewurl.Text; SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "update Resource_path set resource_path=‘" + newUrl + "‘ where resource_id=2"; SqlCommand cmd = new SqlCommand(sql, con); try { con.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } } } //点击关闭触发的事件 private void button3_Click(object sender, EventArgs e) { this.Close(); } private void btnll_Click(object sender, EventArgs e) { DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { txtnewurl.Text = folderBrowserDialog1.SelectedPath + "\\"; } } private void frmsingerphotourl_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select resource_path from Resource_path where resource_id=2"; txturl.Text = song_path(sql); } //给当前路径赋值 private string song_path(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); string path = ""; try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { path = dr["resource_path"].ToString(); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } return path; }
修改歌曲路径(frmsongurl)
private void frmsongurl_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "select resource_path from Resource_path where resource_id=1"; txturl.Text = song_path(sql); } //给当前路径赋值 private string song_path(string sql) { SqlConnection con = new SqlConnection(SqlHelper.str); SqlCommand cmd = new SqlCommand(sql, con); string path = ""; try { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null) { if (dr.HasRows) { while (dr.Read()) { path = dr["resource_path"].ToString(); } } } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } return path; } //点击浏览触发的事件 private void btnll_Click(object sender, EventArgs e) { DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { txtnewurl.Text = folderBrowserDialog1.SelectedPath + "\\"; } } //点击关闭触发的事件 private void button3_Click(object sender, EventArgs e) { this.Close(); } //点击保存触发的事件 private void button2_Click(object sender, EventArgs e) { // 如果新路径为空,提示 if (this.txtnewurl.Text.Trim() == "") { MessageBox.Show("请选择新路径!"); } else { // 用户确认修改 if (MessageBox.Show("确定要修改路径吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Directory.Delete(txtnewurl.Text); Directory.Move(txturl.Text, txtnewurl.Text); string newUrl = txtnewurl.Text; SqlConnection con = new SqlConnection(SqlHelper.str); string sql = "update Resource_path set resource_path=‘" + newUrl + "‘ where resource_id=1"; SqlCommand cmd = new SqlCommand(sql,con); try { con.Open(); int count= cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } } catch (Exception) { MessageBox.Show("网络异常!"); } finally { con.Close(); } } } }