項目名稱: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(); } } } }