用C#,SQL Server编写的音乐播放软件


主界面代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.SqlClient;
 11 
 12 namespace 私属音乐点播
 13 {
 14     public partial class frmMain : Form
 15     {
 16         public frmMain()
 17         {
 18             InitializeComponent();
 19         }
 20         DBHelper db = new DBHelper();
 21         /// <summary>
 22         /// lv的目标界面定义,0歌曲,1歌手,2国家,3性别,
 23         /// 亮点
 24         /// </summary>
 25         int num = 0;    //lv的目标界面定义,0歌曲,1歌手,2国家,3性别,4字数
 26         //单击歌星点歌按钮事件
 27         private void tsbtnTool_singer_Click(object sender, EventArgs e)
 28         {
 29             //目标项在表中的个数
 30             string strCounts=@"select COUNT(1) from singer_info";
 31             //查询歌手Id和歌手名字,私有dr方法
 32             string str = @"select singer_id,singer_name from singer_info";
 33             //获取歌手方法
 34             GetSinger(strCounts, str);
 35         }
 36         //双击lv_Login组件事件
 37         string gender = "";
 38         private void lv_Login_DoubleClick(object sender, EventArgs e)
 39         {
 40             
 41             //亮点
 42             switch (num)
 43             {
 44                 //歌曲双击事件
 45                 case 0:
 46                     int result = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);
 47                     DoubleClickSong(result);
 48                     break;
 49                 //歌手双击事件
 50                 case 1:
 51                     int singerId = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);
 52                     string str = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_info.singer_id=" + singerId + " and song_info.singer_id = singer_info.singer_id";
 53                     num = 0;
 54                     SingerToSongs(str);
 55                     break;
 56                 //国家双击事件
 57                 case 2:
 58                     int type_id = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);
 59                     string strCount = @"select count(1) from singer_info where singer_sex='" + gender + "' and singertype_id=" + type_id + "";
 60                     string strInfo=@"select singer_id,singer_name from singer_info where singer_sex='" + gender + "' and singertype_id=" + type_id + "";
 61                     //下一个lv_Login为歌曲,num=1
 62                     num = 1;
 63                     GetSinger(strCount,strInfo);
 64                     break;
 65                 //性别双击事件
 66                 case 3:
 67                     gender = lv_Login.SelectedItems[0].Text;
 68                     //下一个为歌手地区,num值为2
 69                     num = 2;
 70                     //搞出国家lv方法
 71                     GetSingerType();
 72                     break;
 73                 //字数点歌
 74                 case 4:
 75                     num = 0;
 76                     string playCount;
 77                     if (Convert.ToInt32(lv_Login.SelectedItems[0].Tag)!=11)
 78                     {
 79                         playCount = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_word_count = " + lv_Login.SelectedItems[0].Tag + " and song_info.singer_id = singer_info.singer_id";
 80                     }
 81                     else
 82                     {
 83                         playCount = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_word_count > 10 and song_info.singer_id = singer_info.singer_id";
 84                     }
 85                     SingerToSongs(playCount);
 86                     break;
 87                 //其他非合法组件双击事件,直接跳出
 88                 default:
 89                     //不用思索,直接跳出
 90                     return;
 91             }
 92         }
 93         //类型点歌按钮事件
 94         private void tsbtnTool_type_Click(object sender, EventArgs e)
 95         {
 96             //打开性别lv
 97             //目标lv为性别,所以num为3
 98             num = 3;
 99             imglist.Images.Clear();
100             lv_Login.Clear();
101             lv_Login.Controls.Clear();
102             lv_Login.View = View.LargeIcon;
103             try
104             {
105                 imglist.ImageSize = new Size(166, 168);
106                 imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\male.png"));
107                 imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\female.png"));
108                 imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\couple.png"));
109             }
110             catch (Exception)
111             {
112                 imglist.ImageSize = new Size(100, 100);
113                 ListViewItem male1 = new ListViewItem("");
114                 ListViewItem female1 = new ListViewItem("");
115                 ListViewItem couple1 = new ListViewItem("组合");
116                 lv_Login.Items.Add(male1);
117                 lv_Login.Items.Add(female1);
118                 lv_Login.Items.Add(couple1);
119                 return;
120             }
121             lv_Login.LargeImageList = imglist;
122             ListViewItem male = new ListViewItem("", 0);
123             ListViewItem female = new ListViewItem("", 1);
124             ListViewItem couple = new ListViewItem("组合", 2);
125             lv_Login.Items.Add(male);
126             lv_Login.Items.Add(female);
127             lv_Login.Items.Add(couple);
128         }
129         #region 拼音点歌事件
130         private void tsbtnTool_pinyin_Click(object sender, EventArgs e)
131         {
132             lv_Login.Clear();
133             lv_Login.Controls.Add(txt);
134             lv_Login.Controls.Add(lbl);
135             txt.Location = new Point(200, 10);
136             txt.Size = new System.Drawing.Size(new Point(150, 18));
137             txt.Focus();
138             lbl.Text = "(请输入要查找的歌曲拼音或缩写)";
139             lbl.Font = new Font("幼圆", 10);
140             lbl.ForeColor = Color.Blue;
141             lbl.Location = new Point(350, 10);
142             lbl.Size = new Size(new Point(250, 18));
143             txt.TextChanged += txt_TextChanged;
144             lvNew.Location = new Point(-1, 40);
145             lvNew.Size = new Size(680, 580);
146             lvNew.FullRowSelect = true;
147             lvNew.View = View.Details;
148             lvNew.Columns.Clear();
149             lvNew.Columns.Add("歌曲名称", 300);
150             lvNew.Columns.Add("歌手", 150);
151             lvNew.Columns.Add("点播次数", 100);
152             lvNew.DoubleClick += lvNew_DoubleClick;
153             lv_Login.Controls.Add(lvNew);
154         }
155         //添加三个个控件
156         TextBox txt = new TextBox();
157         Label lbl = new Label();
158         ListView lvNew = new ListView();
159         //lv_Login的新添项lvNew的Items双击事件
160         private void lvNew_DoubleClick(object sender, EventArgs e)
161         {
162             //在这里引用歌曲双击事件
163             int result = Convert.ToInt32(lvNew.SelectedItems[0].Tag);
164             //MessageBox.Show(result.ToString());
165             DoubleClickSong(result);
166         }
167         //new出的txt的text值改变时事件
168         private void txt_TextChanged(object sender, EventArgs e)
169         {
170             lvNew.Items.Clear();
171             string result="";
172             if (!txt.Text.Trim().Equals(string.Empty))
173             {
174                 foreach (char item in txt.Text)
175                 {
176                     if (item>='a'&&item<='z')
177                     {
178                         result += item;
179                         result += '%';
180                     }else if (item>='A'&&item<='Z')
181                     {
182                         result += item.ToString().ToLower();
183                         result += '%';
184                     }
185                 }
186             }
187             string str;
188             if (!result.Equals(string.Empty))
189             {
190                 str = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_ab like '%" + result + "%' and song_info.singer_id = singer_info.singer_id";
191             }
192             else
193             {
194                 return;
195             }
196             try
197             {
198                 SqlDataReader dr = db.Dr(str);
199                 if (dr.HasRows)
200                 {
201                     while (dr.Read())
202                     {
203                         ListViewItem item = new ListViewItem(dr["song_name"].ToString());
204                         item.Tag = dr["song_id"].ToString();
205                         item.SubItems.Add(dr["singer_name"].ToString());
206                         item.SubItems.Add(dr["song_play_count"].ToString());
207                         lvNew.Items.Add(item);
208                     }
209                     dr.Close();
210                 }
211             }
212             catch (Exception)
213             {
214                 MessageBox.Show("私有连接异常!");
215             }
216             finally
217             {
218                 db.CloseConnection();
219             }
220         }
221         #endregion
222         //金榜排行按钮点击事件
223         private void tsbtnTool_order_Click(object sender, EventArgs e)
224         {
225             imglist.Images.Clear();
226             lv_Login.SmallImageList = imglist;
227             string str = @"select top 10 song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_info.singer_id=singer_info.singer_id order by song_play_count desc";
228             num = 0;
229             SingerToSongs(str);
230             lv_Login.View = View.List;
231             imglist.ImageSize = new Size(60, 60);
232             try
233             {
234                 for (int i = 0; i < 10; i++)
235                 {
236                     imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\" + i + ".png"));
237                     lv_Login.Items[i].ImageIndex = i;
238                 }
239             }
240             catch (Exception)
241             {
242                 
243             }
244         }
245         //单击字数点歌按钮事件
246         private void tsbtnTool_counts_Click(object sender, EventArgs e)
247         {
248             //目标lv_Login为字数lv, num为4
249             num = 4;
250             lv_Login.Clear();
251             lv_Login.Controls.Clear();
252             imglist.Images.Clear();
253             lv_Login.LargeImageList = imglist;
254             imglist.ImageSize = new System.Drawing.Size(60, 60);
255             lv_Login.View = View.LargeIcon;
256             for (int i = 1; i < 11; i++)
257             {
258                 imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\" + i + ".png"));
259                 ListViewItem item = new ListViewItem((i) + "个字",i-1);
260                 item.Tag = i+1;
261                 lv_Login.Items.Add(item);
262             }
263             imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\N.png"));
264             ListViewItem itemN = new ListViewItem("N个字", 10);
265             itemN.Tag = 11;
266             lv_Login.Items.Add(itemN);
267         }
268         //定时器事件
269         private void timer_Tick(object sender, EventArgs e)
270         {
271             Playing();
272         }
273         /// <summary>
274         /// 歌手双击方法
275         /// </summary>
276         /// <param name="str">查询歌曲语句</param>
277         public void SingerToSongs(string str)
278         {
279             num = 0;
280             lv_Login.View = View.Details;
281             lv_Login.Clear();
282             lv_Login.Controls.Clear();
283             lv_Login.Columns.Add("歌曲名称", 300);
284             lv_Login.Columns.Add("歌手", 150);
285             lv_Login.Columns.Add("点播次数", 100);
286             try
287             {
288                 SqlDataReader dr = db.Dr(str);
289                 if (dr.HasRows)
290                 {
291                     while (dr.Read())
292                     {
293                         ListViewItem item = new ListViewItem(dr["song_name"].ToString());
294                         item.Tag = dr["song_id"].ToString();
295                         item.SubItems.Add(dr["singer_name"].ToString());
296                         item.SubItems.Add(dr["song_play_count"].ToString());
297                         lv_Login.Items.Add(item);
298                     }
299                     dr.Close();
300                 }
301             }
302             catch (Exception)
303             {
304                 MessageBox.Show("私有连接异常!");
305             }
306             finally
307             {
308                 db.CloseConnection();
309             }
310         }
311         /// <summary>
312         /// 播放列表专属数组
313         /// </summary>
314         Song[] playing_list = new Song[100];
315         /// <summary>
316         /// 歌曲双击方法
317         /// </summary>
318         /// <param name="result">双击歌曲项的Tag值</param>
319         public void DoubleClickSong(int result)
320         {
321             //循环判定
322             foreach (Song song in playing_list)
323             {
324                 if (song!=null && song.SongId == result.ToString())
325                 {
326                     MessageBox.Show("已点!");
327                     return;
328                 }
329                 else if (song==null)
330                 {
331                     break;
332                 }
333             }
334             int i = 0;
335             foreach (Song one in playing_list)
336             {
337                 if (one != null)
338                     i++;
339             }
340             string str = @"select song_name from song_info where song_id = " + result + "";
341             try
342             {
343                 SqlDataReader dr = db.Dr(str);
344                 if (dr.HasRows)
345                 {
346                     if (dr.Read())
347                     {
348                         playing_list[i] = new Song();
349                         playing_list[i].SongName = dr["song_name"].ToString();
350                         playing_list[i].SongId = result.ToString();
351                     }
352                 }
353                 dr.Close();
354             }
355             catch (Exception)
356             {
357                 MessageBox.Show("私有连接异常!");
358             }
359             finally
360             {
361                 db.CloseConnection();
362                 if (playing_list[i]!=null)
363                 {
364                     playing_list[i].Songurl = GetSongurlById(result.ToString());
365                 }
366             }
367             ListViewItem item = new ListViewItem(playing_list[i].SongName);
368             ///注意
369             ///注意
370             if (i == 0)
371             {
372                 item.SubItems.Add("正在播放");
373                 wmp.URL = playing_list[i].Songurl;
374                 try
375                 {
376                     SetPlayCount(playing_list[i].SongId);
377                 }
378                 catch (Exception ex)
379                 {
380                     MessageBox.Show(ex.Message);
381                 }
382             }
383             else
384             {
385                 item.SubItems.Add("未播放");
386                 try
387                 {
388                     SetPlayCount(playing_list[i].SongId);
389                 }
390                 catch (Exception ex)
391                 {
392                     MessageBox.Show(ex.Message);
393                 }
394             }
395             item.SubItems.Add(playing_list[i].SongId);
396             item.Tag = playing_list[i].Songurl;
397             lvPlay_list.Items.Add(item);
398             timer.Enabled = true;
399         }
400         /// 当前播放的歌曲在对象数组中的下标
401         /// </summary>
402         int playingIndex = 0;
403         /// <summary>
404         /// 播放方法
405         /// </summary>
406         public void Playing()
407         {
408             //重新写
409             if (wmp.playState==WMPLib.WMPPlayState.wmppsPlaying)
410             {
411                 try
412                 {
413                     //面板信息更改
414                     lblPlaying_name.Text = playing_list[playingIndex].SongName;
415                     lblNext_name.Text = playing_list[playingIndex+1].SongName;
416                 }
417                 catch (Exception)
418                 {
419                     lblNext_name.Text = "";
420                 }
421             }
422             else if (wmp.playState == WMPLib.WMPPlayState.wmppsStopped)
423             {
424                 try
425                 {
426                     //状态更新
427                     lvPlay_list.Items[playingIndex].SubItems[1].Text = "已播放";
428                 }
429                 catch (Exception)
430                 {
431                     return;
432                 }
433                 playingIndex++;
434                 //为播放列表的最后一个时,返回index,跳出方法
435                 if (playing_list[playingIndex] == null)
436                 {
437                     playingIndex--;
438                     wmp.URL = string.Empty;
439                     return;
440                 }
441                 wmp.URL = playing_list[playingIndex].Songurl;
442                 lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";
443                 //面板提示信息更新
444                 lblPlaying_name.Text = playing_list[playingIndex].SongName;
445                 try
446                 {
447                     lblNext_name.Text = playing_list[playingIndex + 1].SongName;
448                 }
449                 catch (Exception)
450                 {
451                     lblNext_name.Text = "";
452                 }
453             }
454             //歌曲读取异常(为空,或,路径异常)
455             else if (wmp.playState==WMPLib.WMPPlayState.wmppsReady)
456             {
457                 if (wmp.URL==string.Empty)
458                 {
459                     //只要路径为空,只需要判定播放列表是否为空,否则直接播放
460                     if (playing_list[playingIndex] == null)
461                     {
462                         return;
463                     }
464                     wmp.URL = playing_list[playingIndex].Songurl;
465                     try
466                     {
467                         lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";
468                     }
469                     catch (Exception)
470                     {
471                         return;
472                     }
473                     //面板提示信息更新
474                     lblPlaying_name.Text = playing_list[playingIndex].SongName;
475                     try
476                     {
477                         lblNext_name.Text = playing_list[playingIndex +1].SongName;
478                     }
479                     catch (Exception)
480                     {
481                         lblNext_name.Text = "";
482                     }
483                 }
484                 else
485                 {
486                     lvPlay_list.Items[playingIndex].SubItems[1].Text = "路径异常";
487                     playingIndex++;
488                     //为播放列表的最后一个时,返回index,跳出方法
489                     if (playing_list[playingIndex] == null)
490                     {
491                         wmp.URL = string.Empty;
492                         return;
493                     }
494                     wmp.URL = playing_list[playingIndex].Songurl;
495                     try
496                     {
497                         lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";
498                     }
499                     catch (Exception)
500                     {
501                         return;
502                     }
503                     
504                     //面板提示信息更新
505                     lblPlaying_name.Text = playing_list[playingIndex].SongName;
506                     try
507                     {
508                         lblNext_name.Text = playing_list[playingIndex + 1].SongName;
509                     }
510                     catch (Exception)
511                     {
512                         lblNext_name.Text = "";
513                     }
514                 }
515             }
516             else
517             {
518                 return;
519             }
520         }
521         /// <summary>
522         /// 根据歌手id查找歌手图片路径
523         /// </summary>
524         /// <param name="id">singerId</param>
525         /// <returns>singerPhotoUrl</returns>
526         public string GetSingerurlById(string id)
527         {
528             string str2 = @"select resource_path from resource where resource_type='歌手图片地址'";
529             string singerphoto_url = db.ScalForString(str2);
530             string str = @"select singer_photo_url from singer_info where singer_id='" + id + "'";
531             string singer_url = db.ScalForString(str);
532             string sb = singerphoto_url + singer_url;
533             return sb;
534         }
535         /// <summary>
536         /// 根据歌曲id查找歌曲路径
537         /// </summary>
538         /// <param name="id">songId</param>
539         /// <returns>songUrl</returns>
540         public string GetSongurlById(string id)
541         {
542             string str = @"select resource_path from resource where resource_type = '歌曲地址'";
543             StringBuilder sb = new StringBuilder(db.ScalForString(str));
544             string str2 = @"select song_url from song_info where song_id = " + id + "";
545             sb.Append(db.ScalForString(str2));
546             return sb.ToString();
547         }
548         /// <summary>
549         /// 播放次数更新方法
550         /// </summary>
551         /// <param name="songId">歌曲Id的字符串格式</param>
552         /// <returns>是否成功</returns>
553         public bool SetPlayCount(string songId)
554         {
555             int result;
556             string str1 = @"select song_play_count from song_info where song_id=" + songId + "";
557             try
558             {
559                 result = Convert.ToInt32(db.Scal(str1));
560             }
561             catch (Exception)
562             {
563                 result = 0;
564             }
565             result++;
566             string str = @"update song_info set song_play_count = " + result + " where song_id=" + songId + "";
567             if (db.Non(str)>0)
568             {
569                 return true;
570             }
571             else
572             {
573                 return false;
574             }
575         }
576         /// <summary>
577         /// new国家lv_Login方法
578         /// </summary>
579         private void GetSingerType()
580         {
581             lv_Login.Clear();
582             lv_Login.Controls.Clear();
583             imglist.Images.Clear();
584             lv_Login.View = View.LargeIcon;
585             lv_Login.LargeImageList = imglist;
586             string str1 = @"select count(1) from singer_type";
587             int result;
588             try
589             {
590                 result = Convert.ToInt32(db.Scal(str1));
591             }
592             catch (Exception)
593             {
594                 return;
595             }
596             string str = @"select singertype_id, singertype_name from singer_type";
597             try
598             {
599                 SqlDataReader dr = db.Dr(str);
600                 if (dr.HasRows)
601                 {
602                     for (int i = 0; i < result; i++)
603                     {
604                         dr.Read();
605                         Image img;
606                         try
607                         {
608                             img = Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image" + Convert.ToString(dr["singertype_name"]) + ".png");
609                         }
610                         catch (Exception)
611                         {
612                             img = Image.FromFile("D:\\课程\\S1\\KTV\\MyKTV\\image\\singer_temp.jpg");
613                         }
614                         imglist.Images.Add(img);
615                         ListViewItem item = new ListViewItem(dr["singertype_name"].ToString(),i);
616                         item.Tag = Convert.ToString(dr["singertype_id"]);
617                         lv_Login.Items.Add(item);
618                     }
619                 }
620                 dr.Close();
621             }
622             catch (Exception)
623             {
624                 MessageBox.Show("歌手地区读取异常!");
625             }
626             finally
627             {
628                 db.CloseConnection();
629             }
630         }
631         /// <summary>
632         /// 获取歌手信息
633         /// </summary>
634         /// <param name="strCounts">条件下歌手个数查询语句</param>
635         /// <param name="str">条件下歌手信息查询语句</param>
636         private void GetSinger(string strCounts, string str)
637         {
638             //歌手lv的目标lv为歌曲
639             num = 1;
640             imglist.Images.Clear();
641             lv_Login.Clear();
642             lv_Login.Controls.Clear();
643             lv_Login.View = View.LargeIcon;
644             //确定大图标的图像列表对象
645             lv_Login.LargeImageList = imglist;
646             imglist.ImageSize = new System.Drawing.Size(72, 128);
647             int result = db.ScalForInt(strCounts);
648             SqlCommand comm = new SqlCommand(str, db.conn);
649             //new出对象数组,用于存储目标表格信息
650             Song[] song = new Song[result];
651             try
652             {
653                 db.OpenConnection();
654                 SqlDataReader dr = comm.ExecuteReader();
655                 if (dr != null)
656                     if (dr.HasRows)
657                     {
658                         for (int i = 0; i < result; i++)
659                         {
660                             if (dr.Read())
661                             {
662                                 //读取了歌手id,歌手名称
663                                 song[i] = new Song();
664                                 song[i].SingerId = dr["singer_id"].ToString();
665                                 song[i].SingerName = dr["singer_name"].ToString();
666                             }
667                         }
668                         dr.Close();
669                     }
670             }
671             catch (Exception)
672             {
673                 MessageBox.Show("私有连接异常!");
674             }
675             finally
676             {
677                 db.CloseConnection();
678             }
679 
680             //改变imagelist的图片大小:imglist.ImageSize = new Size(32, 32);
681 
682             //循环读出图片url,加载对应图片到imglist,给items组合项添加对象数组中每一项
683             for (int i = 0; i < result; i++)
684             {
685                 song[i].SingerPhoto = GetSingerurlById(song[i].SingerId);
686                 Image img;
687                 //亮点
688                 try
689                 {
690                     img = Image.FromFile(song[i].SingerPhoto);
691                 }
692                 catch (Exception)
693                 {
694                     img = Image.FromFile("D:\\课程\\S1\\KTV\\MyKTV\\image\\singer_temp.jpg");
695                 }
696                 imglist.Images.Add(img);
697                 ListViewItem item = new ListViewItem(song[i].SingerName, i);
698                 item.Tag = song[i].SingerId;
699                 lv_Login.Items.Add(item);
700             }
701         }
702         #region 删除播放列表歌曲,双击播放
703         ////删除选中歌曲按钮事件
704         private void btnDel_Click(object sender, EventArgs e)
705         {
706             int result;
707             try
708             {
709                 result = Convert.ToInt32(lvPlay_list.SelectedItems[0].Index);
710             }
711             catch (Exception)
712             {
713                 return;
714             }
715             //清空对象数组与列表
716             lvPlay_list.SelectedItems[0].Remove();
717             ///亮点
718             if (result == playingIndex)
719             {
720 
721                 try
722                 {
723                     if (playing_list[result + 1] != null)
724                     {
725                         for (int i = result + 1; i < playing_list.Length; i++)
726                         {
727                             playing_list[i - 1].SongId = playing_list[i].SongId;
728                             playing_list[i - 1].SongName = playing_list[i].SongName;
729                             playing_list[i - 1].Songurl = playing_list[i].Songurl;
730                         }
731                         wmp.URL = playing_list[result].Songurl;
732                     }
733                     else
734                     {
735                         playing_list[result] = playing_list[result + 1];
736                         wmp.URL = string.Empty;
737                     }
738                 }
739                 catch (Exception)
740                 {
741                     wmp.URL = string.Empty;
742                 }
743             }
744            
745         }
746         ////双击播放列表组件时
747         private void lvPlay_list_DoubleClick(object sender, EventArgs e)
748         {
749             try
750             {
751                 wmp.URL = playing_list[Convert.ToInt32(lvPlay_list.SelectedItems[0].Index)].Songurl;
752                 lvPlay_list.Items[playingIndex].SubItems[1].Text = "已播放";
753                 playingIndex = Convert.ToInt32(lvPlay_list.SelectedItems[0].Index);
754                 lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";
755             }
756             catch (Exception)
757             {
758                 return;
759             }
760         }
761         ////列表循环方式按钮点击事件
762         private void btnState_Click(object sender, EventArgs e)
763         {
764         //    Timer timeResult = new Timer();
765         //    if (btnState.Text=="列表循环模式")
766         //    {
767         //        btnState.Text = "单曲循环模式";
768         //        timer.Enabled = false;
769         //        timeResult.Enabled = true;
770         //        timeResult.Interval = 1000;
771         //        timeResult.Tick += timeResult_Tick;
772         //    }
773         //    else if (btnState.Text=="单曲循环模式")
774         //    {
775         //        btnState.Text = "无循环模式";
776         //        timer.Enabled = true;
777         //        timeResult.Enabled = false;
778         //    }
779         //    else if (btnState.Text=="无循环模式")
780         //    {
781         //        btnState.Text = "列表循环模式";
782         //        timer.Enabled = false;
783         //        timeResult.Enabled = true;
784         //        timeResult.Interval = 1000;
785         //        timeResult.Tick += timeResult_Tick;
786         //    }
787         }
788         ////附加定时器事件
789         void timeResult_Tick(object sender, EventArgs e)
790         {
791 
792         //    if (wmp.playState==WMPLib.WMPPlayState.wmppsStopped&&btnState.Text=="单曲循环模式")
793         //    {
794         //        wmp.URL = playing_list[playingIndex].Songurl;
795         //    }
796         //    else if (btnState.Text=="列表循环模式")
797         //    {
798         //        int result=-1;
799         //        foreach (Song item in playing_list)
800         //        {
801         //            if (item!=null)
802         //            {
803         //                result++;
804         //            }
805         //            else
806         //            {
807         //                break;
808         //            }
809         //        }
810         //        if (playingIndex==result)
811         //        {
812         //            playingIndex = 0;
813         //        }
814         //    }
815         }
816         #endregion
817 
818     }
819 }
View Code
数据库连接辅助代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Data.SqlClient;
  7 using System.Data;
  8 
  9 namespace 私属音乐点播
 10 {
 11     class DBHelper
 12     {
 13         public static string sql = @"Data source=.;initial catalog=KTV;uid=sa;pwd=liuzs";
 14         public SqlConnection conn = new SqlConnection(sql);
 15         //打开数据库连接
 16         public void OpenConnection()
 17         {
 18             if (conn.State == ConnectionState.Closed)
 19             {
 20                 conn.Open();
 21 
 22             }
 23             else if (conn.State == ConnectionState.Broken)
 24             {
 25                 conn.Close();
 26                 conn.Open();
 27             }
 28         }
 29         //关闭数据库连接
 30         public void CloseConnection()
 31         {
 32             if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Broken)
 33             {
 34                 conn.Close();
 35             }
 36         }
 37         //返回受影响行号方法
 38         public int Non(string str)
 39         {
 40             SqlCommand comm = new SqlCommand(str,conn);
 41             int result =0 ;
 42             try
 43             {
 44                 OpenConnection();
 45                 result = Convert.ToInt32(comm.ExecuteNonQuery());
 46             }
 47             catch (Exception)
 48             {
 49                 System.Windows.Forms.MessageBox.Show("获取表行号网络异常!");
 50             }
 51             finally
 52             {
 53                 CloseConnection();
 54             }
 55             return result;
 56         }
 57         //返回查询首行首列为Int值方法
 58         public int ScalForInt(string str)
 59         {
 60             SqlCommand comm = new SqlCommand(str,conn);
 61             int result = 0;
 62             try
 63             {
 64                 OpenConnection();
 65                 result = Convert.ToInt32(comm.ExecuteScalar());
 66             }
 67             catch (Exception)
 68             {
 69                 System.Windows.Forms.MessageBox.Show("获取整型查询值网络异常!");
 70             }
 71             finally
 72             {
 73                 CloseConnection();
 74             }
 75             return result;
 76         }
 77         //返回查询首行首列为String值方法
 78         public string ScalForString(string str)
 79         {
 80             SqlCommand comm = new SqlCommand(str,conn);
 81             string result = "";
 82             try
 83             {
 84                 OpenConnection();
 85                 result = Convert.ToString(comm.ExecuteScalar());
 86             }
 87             catch (Exception)
 88             {
 89                 System.Windows.Forms.MessageBox.Show("获取字符串查询值网络异常!");
 90             }
 91             finally
 92             {
 93                 CloseConnection();
 94             }
 95             return result;
 96         }
 97         //返回查询首行首列值方法
 98         public object Scal(string str)
 99         {
100             SqlCommand comm = new SqlCommand(str,conn);
101             object result = null;
102             try
103             {
104                 OpenConnection();
105                 result = comm.ExecuteScalar();
106             }
107             catch (Exception)
108             {
109                 System.Windows.Forms.MessageBox.Show("获取查询值网络异常!");
110             }
111             finally
112             {
113                 CloseConnection();
114             }
115             return result;
116         }
117         /// <summary>
118         /// 返回DataReader方法
119         /// 需要手动关闭读取器,关闭连接
120         /// 不可与其他数据库查询方法连用,da除外
121         /// </summary>
122         /// <param name="str">查询语句</param>
123         /// <returns>dr对象</returns>
124         public SqlDataReader Dr(string str)
125         {
126             SqlCommand comm = new SqlCommand(str,conn);
127             SqlDataReader dr = null;
128             try
129             {
130                 OpenConnection();
131                 dr = comm.ExecuteReader();
132             }
133             catch (Exception)
134             {
135                 System.Windows.Forms.MessageBox.Show("获取查询值集合网络异常!");
136             }
137             return dr;
138         }
139         //返回DataAdapter方法
140         public SqlDataAdapter Da(string str)
141         {
142             SqlDataAdapter da = null;
143             try
144             {
145                 da = new SqlDataAdapter(str, conn);
146             }
147             catch (Exception)
148             {
149                 System.Windows.Forms.MessageBox.Show("获取表单网络异常!");
150             }
151             finally
152             {
153                 CloseConnection();
154             }
155             return da;
156         }
157 
158     }
159 }
View Code

写这些代码也是我在S1的总结,虽然不是半年来学习的全部,但也是大部分了.

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;
namespace 私属音乐点播{    public partial class frmMain : Form    {        public frmMain()        {            InitializeComponent();        }        DBHelper db = new DBHelper();        /// <summary>        /// lv的目标界面定义,0歌曲,1歌手,2国家,3性别,        /// 亮点        /// </summary>        int num = 0;    //lv的目标界面定义,0歌曲,1歌手,2国家,3性别,4字数        //单击歌星点歌按钮事件        private void tsbtnTool_singer_Click(object sender, EventArgs e)        {            //目标项在表中的个数            string strCounts=@"select COUNT(1) from singer_info";            //查询歌手Id和歌手名字,私有dr方法            string str = @"select singer_id,singer_name from singer_info";            //获取歌手方法            GetSinger(strCounts, str);        }        //双击lv_Login组件事件        string gender = "男";        private void lv_Login_DoubleClick(object sender, EventArgs e)        {                        //亮点            switch (num)            {                //歌曲双击事件                case 0:                    int result = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);                    DoubleClickSong(result);                    break;                //歌手双击事件                case 1:                    int singerId = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);                    string str = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_info.singer_id=" + singerId + " and song_info.singer_id = singer_info.singer_id";                    num = 0;                    SingerToSongs(str);                    break;                //国家双击事件                case 2:                    int type_id = Convert.ToInt32(lv_Login.SelectedItems[0].Tag);                    string strCount = @"select count(1) from singer_info where singer_sex='" + gender + "' and singertype_id=" + type_id + "";                    string strInfo=@"select singer_id,singer_name from singer_info where singer_sex='" + gender + "' and singertype_id=" + type_id + "";                    //下一个lv_Login为歌曲,num=1                    num = 1;                    GetSinger(strCount,strInfo);                    break;                //性别双击事件                case 3:                    gender = lv_Login.SelectedItems[0].Text;                    //下一个为歌手地区,num值为2                    num = 2;                    //搞出国家lv方法                    GetSingerType();                    break;                //字数点歌                case 4:                    num = 0;                    string playCount;                    if (Convert.ToInt32(lv_Login.SelectedItems[0].Tag)!=11)                    {                        playCount = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_word_count = " + lv_Login.SelectedItems[0].Tag + " and song_info.singer_id = singer_info.singer_id";                    }                    else                    {                        playCount = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_word_count > 10 and song_info.singer_id = singer_info.singer_id";                    }                    SingerToSongs(playCount);                    break;                //其他非合法组件双击事件,直接跳出                default:                    //不用思索,直接跳出                    return;            }        }        //类型点歌按钮事件        private void tsbtnTool_type_Click(object sender, EventArgs e)        {            //打开性别lv            //目标lv为性别,所以num为3            num = 3;            imglist.Images.Clear();            lv_Login.Clear();            lv_Login.Controls.Clear();            lv_Login.View = View.LargeIcon;            try            {                imglist.ImageSize = new Size(166, 168);                imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\male.png"));                imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\female.png"));                imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\couple.png"));            }            catch (Exception)            {                imglist.ImageSize = new Size(100, 100);                ListViewItem male1 = new ListViewItem("男");                ListViewItem female1 = new ListViewItem("女");                ListViewItem couple1 = new ListViewItem("组合");                lv_Login.Items.Add(male1);                lv_Login.Items.Add(female1);                lv_Login.Items.Add(couple1);                return;            }            lv_Login.LargeImageList = imglist;            ListViewItem male = new ListViewItem("男", 0);            ListViewItem female = new ListViewItem("女", 1);            ListViewItem couple = new ListViewItem("组合", 2);            lv_Login.Items.Add(male);            lv_Login.Items.Add(female);            lv_Login.Items.Add(couple);        }        #region 拼音点歌事件        private void tsbtnTool_pinyin_Click(object sender, EventArgs e)        {            lv_Login.Clear();            lv_Login.Controls.Add(txt);            lv_Login.Controls.Add(lbl);            txt.Location = new Point(200, 10);            txt.Size = new System.Drawing.Size(new Point(150, 18));            txt.Focus();            lbl.Text = "(请输入要查找的歌曲拼音或缩写)";            lbl.Font = new Font("幼圆", 10);            lbl.ForeColor = Color.Blue;            lbl.Location = new Point(350, 10);            lbl.Size = new Size(new Point(250, 18));            txt.TextChanged += txt_TextChanged;            lvNew.Location = new Point(-1, 40);            lvNew.Size = new Size(680, 580);            lvNew.FullRowSelect = true;            lvNew.View = View.Details;            lvNew.Columns.Clear();            lvNew.Columns.Add("歌曲名称", 300);            lvNew.Columns.Add("歌手", 150);            lvNew.Columns.Add("点播次数", 100);            lvNew.DoubleClick += lvNew_DoubleClick;            lv_Login.Controls.Add(lvNew);        }        //添加三个个控件        TextBox txt = new TextBox();        Label lbl = new Label();        ListView lvNew = new ListView();        //lv_Login的新添项lvNew的Items双击事件        private void lvNew_DoubleClick(object sender, EventArgs e)        {            //在这里引用歌曲双击事件            int result = Convert.ToInt32(lvNew.SelectedItems[0].Tag);            //MessageBox.Show(result.ToString());            DoubleClickSong(result);        }        //new出的txt的text值改变时事件        private void txt_TextChanged(object sender, EventArgs e)        {            lvNew.Items.Clear();            string result="";            if (!txt.Text.Trim().Equals(string.Empty))        {        foreach (char item in txt.Text)            {            if (item>='a'&&item<='z')                {                        result += item;                        result += '%';                }else if (item>='A'&&item<='Z')                {                        result += item.ToString().ToLower();                        result += '%';                }            }        }            string str;            if (!result.Equals(string.Empty))            {                str = @"select song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_ab like '%" + result + "%' and song_info.singer_id = singer_info.singer_id";            }            else            {                return;            }            try            {                SqlDataReader dr = db.Dr(str);                if (dr.HasRows)                {                    while (dr.Read())                    {                        ListViewItem item = new ListViewItem(dr["song_name"].ToString());                        item.Tag = dr["song_id"].ToString();                        item.SubItems.Add(dr["singer_name"].ToString());                        item.SubItems.Add(dr["song_play_count"].ToString());                        lvNew.Items.Add(item);                    }                    dr.Close();                }            }            catch (Exception)            {                MessageBox.Show("私有连接异常!");            }            finally            {                db.CloseConnection();            }        }        #endregion        //金榜排行按钮点击事件        private void tsbtnTool_order_Click(object sender, EventArgs e)        {            imglist.Images.Clear();            lv_Login.SmallImageList = imglist;            string str = @"select top 10 song_id,song_name,singer_name,song_play_count from song_info,singer_info where song_info.singer_id=singer_info.singer_id order by song_play_count desc";            num = 0;            SingerToSongs(str);            lv_Login.View = View.List;            imglist.ImageSize = new Size(60, 60);            try            {                for (int i = 0; i < 10; i++)                {                    imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\" + i + ".png"));                    lv_Login.Items[i].ImageIndex = i;                }            }            catch (Exception)            {                            }        }        //单击字数点歌按钮事件        private void tsbtnTool_counts_Click(object sender, EventArgs e)        {            //目标lv_Login为字数lv, num为4            num = 4;            lv_Login.Clear();            lv_Login.Controls.Clear();            imglist.Images.Clear();            lv_Login.LargeImageList = imglist;            imglist.ImageSize = new System.Drawing.Size(60, 60);            lv_Login.View = View.LargeIcon;            for (int i = 1; i < 11; i++)            {                imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\" + i + ".png"));                ListViewItem item = new ListViewItem((i) + "个字",i-1);                item.Tag = i+1;                lv_Login.Items.Add(item);            }            imglist.Images.Add(Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image\N.png"));            ListViewItem itemN = new ListViewItem("N个字", 10);            itemN.Tag = 11;            lv_Login.Items.Add(itemN);        }        //定时器事件        private void timer_Tick(object sender, EventArgs e)        {            Playing();        }        /// <summary>        /// 歌手双击方法        /// </summary>        /// <param name="str">查询歌曲语句</param>        public void SingerToSongs(string str)        {            num = 0;            lv_Login.View = View.Details;            lv_Login.Clear();            lv_Login.Controls.Clear();            lv_Login.Columns.Add("歌曲名称", 300);            lv_Login.Columns.Add("歌手", 150);            lv_Login.Columns.Add("点播次数", 100);            try            {                SqlDataReader dr = db.Dr(str);                if (dr.HasRows)                {                    while (dr.Read())                    {                        ListViewItem item = new ListViewItem(dr["song_name"].ToString());                        item.Tag = dr["song_id"].ToString();                        item.SubItems.Add(dr["singer_name"].ToString());                        item.SubItems.Add(dr["song_play_count"].ToString());                        lv_Login.Items.Add(item);                    }                    dr.Close();                }            }            catch (Exception)            {                MessageBox.Show("私有连接异常!");            }            finally            {                db.CloseConnection();            }        }        /// <summary>        /// 播放列表专属数组        /// </summary>        Song[] playing_list = new Song[100];        /// <summary>        /// 歌曲双击方法        /// </summary>        /// <param name="result">双击歌曲项的Tag值</param>        public void DoubleClickSong(int result)        {            //循环判定            foreach (Song song in playing_list)            {                if (song!=null && song.SongId == result.ToString())                {                    MessageBox.Show("已点!");                    return;                }                else if (song==null)                {                    break;                }            }            int i = 0;            foreach (Song one in playing_list)            {                if (one != null)                    i++;            }            string str = @"select song_name from song_info where song_id = " + result + "";            try            {                SqlDataReader dr = db.Dr(str);                if (dr.HasRows)                {                    if (dr.Read())                    {                        playing_list[i] = new Song();                        playing_list[i].SongName = dr["song_name"].ToString();                        playing_list[i].SongId = result.ToString();                    }                }                dr.Close();            }            catch (Exception)            {                MessageBox.Show("私有连接异常!");            }            finally            {                db.CloseConnection();                if (playing_list[i]!=null)                {                    playing_list[i].Songurl = GetSongurlById(result.ToString());                }            }            ListViewItem item = new ListViewItem(playing_list[i].SongName);            ///注意            ///注意            if (i == 0)            {                item.SubItems.Add("正在播放");                wmp.URL = playing_list[i].Songurl;                try                {                    SetPlayCount(playing_list[i].SongId);                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message);                }            }            else            {                item.SubItems.Add("未播放");                try                {                    SetPlayCount(playing_list[i].SongId);                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message);                }            }            item.SubItems.Add(playing_list[i].SongId);            item.Tag = playing_list[i].Songurl;            lvPlay_list.Items.Add(item);            timer.Enabled = true;        }        /// 当前播放的歌曲在对象数组中的下标        /// </summary>        int playingIndex = 0;        /// <summary>        /// 播放方法        /// </summary>        public void Playing()        {            //重新写            if (wmp.playState==WMPLib.WMPPlayState.wmppsPlaying)            {                try                {                    //面板信息更改                    lblPlaying_name.Text = playing_list[playingIndex].SongName;                    lblNext_name.Text = playing_list[playingIndex+1].SongName;                }                catch (Exception)                {                    lblNext_name.Text = "无";                }            }            else if (wmp.playState == WMPLib.WMPPlayState.wmppsStopped)            {                try                {                    //状态更新                    lvPlay_list.Items[playingIndex].SubItems[1].Text = "已播放";                }                catch (Exception)                {                    return;                }                playingIndex++;                //为播放列表的最后一个时,返回index,跳出方法                if (playing_list[playingIndex] == null)                {                    playingIndex--;                    wmp.URL = string.Empty;                    return;                }                wmp.URL = playing_list[playingIndex].Songurl;                lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";                //面板提示信息更新                lblPlaying_name.Text = playing_list[playingIndex].SongName;                try                {                    lblNext_name.Text = playing_list[playingIndex + 1].SongName;                }                catch (Exception)                {                    lblNext_name.Text = "无";                }            }            //歌曲读取异常(为空,或,路径异常)            else if (wmp.playState==WMPLib.WMPPlayState.wmppsReady)            {                if (wmp.URL==string.Empty)                {                    //只要路径为空,只需要判定播放列表是否为空,否则直接播放                    if (playing_list[playingIndex] == null)                    {                        return;                    }                    wmp.URL = playing_list[playingIndex].Songurl;                    try                    {                        lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";                    }                    catch (Exception)                    {                        return;                    }                    //面板提示信息更新                    lblPlaying_name.Text = playing_list[playingIndex].SongName;                    try                    {                        lblNext_name.Text = playing_list[playingIndex +1].SongName;                    }                    catch (Exception)                    {                        lblNext_name.Text = "无";                    }                }                else                {                    lvPlay_list.Items[playingIndex].SubItems[1].Text = "路径异常";                    playingIndex++;                    //为播放列表的最后一个时,返回index,跳出方法                    if (playing_list[playingIndex] == null)                    {                        wmp.URL = string.Empty;                        return;                    }                    wmp.URL = playing_list[playingIndex].Songurl;                    try                    {                        lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";                    }                    catch (Exception)                    {                        return;                    }                                        //面板提示信息更新                    lblPlaying_name.Text = playing_list[playingIndex].SongName;                    try                    {                        lblNext_name.Text = playing_list[playingIndex + 1].SongName;                    }                    catch (Exception)                    {                        lblNext_name.Text = "无";                    }                }            }            else            {                return;            }        }        /// <summary>        /// 根据歌手id查找歌手图片路径        /// </summary>        /// <param name="id">singerId</param>        /// <returns>singerPhotoUrl</returns>        public string GetSingerurlById(string id)        {            string str2 = @"select resource_path from resource where resource_type='歌手图片地址'";            string singerphoto_url = db.ScalForString(str2);            string str = @"select singer_photo_url from singer_info where singer_id='" + id + "'";            string singer_url = db.ScalForString(str);            string sb = singerphoto_url + singer_url;            return sb;        }        /// <summary>        /// 根据歌曲id查找歌曲路径        /// </summary>        /// <param name="id">songId</param>        /// <returns>songUrl</returns>        public string GetSongurlById(string id)        {            string str = @"select resource_path from resource where resource_type = '歌曲地址'";            StringBuilder sb = new StringBuilder(db.ScalForString(str));            string str2 = @"select song_url from song_info where song_id = " + id + "";            sb.Append(db.ScalForString(str2));            return sb.ToString();        }        /// <summary>        /// 播放次数更新方法        /// </summary>        /// <param name="songId">歌曲Id的字符串格式</param>        /// <returns>是否成功</returns>        public bool SetPlayCount(string songId)        {            int result;            string str1 = @"select song_play_count from song_info where song_id=" + songId + "";            try            {                result = Convert.ToInt32(db.Scal(str1));            }            catch (Exception)            {                result = 0;            }            result++;            string str = @"update song_info set song_play_count = " + result + " where song_id=" + songId + "";            if (db.Non(str)>0)            {                return true;            }            else            {                return false;            }        }        /// <summary>        /// new国家lv_Login方法        /// </summary>        private void GetSingerType()        {            lv_Login.Clear();            lv_Login.Controls.Clear();            imglist.Images.Clear();            lv_Login.View = View.LargeIcon;            lv_Login.LargeImageList = imglist;            string str1 = @"select count(1) from singer_type";            int result;            try            {                result = Convert.ToInt32(db.Scal(str1));            }            catch (Exception)            {                return;            }            string str = @"select singertype_id, singertype_name from singer_type";            try            {                SqlDataReader dr = db.Dr(str);                if (dr.HasRows)                {                    for (int i = 0; i < result; i++)                    {                        dr.Read();                        Image img;                        try                        {                            img = Image.FromFile(@"D:\课程\S1\KTV\MyKTV\image" + Convert.ToString(dr["singertype_name"]) + ".png");                        }                        catch (Exception)                        {                            img = Image.FromFile("D:\\课程\\S1\\KTV\\MyKTV\\image\\singer_temp.jpg");                        }                        imglist.Images.Add(img);                        ListViewItem item = new ListViewItem(dr["singertype_name"].ToString(),i);                        item.Tag = Convert.ToString(dr["singertype_id"]);                        lv_Login.Items.Add(item);                    }                }                dr.Close();            }            catch (Exception)            {                MessageBox.Show("歌手地区读取异常!");            }            finally            {                db.CloseConnection();            }        }        /// <summary>        /// 获取歌手信息        /// </summary>        /// <param name="strCounts">条件下歌手个数查询语句</param>        /// <param name="str">条件下歌手信息查询语句</param>        private void GetSinger(string strCounts, string str)        {            //歌手lv的目标lv为歌曲            num = 1;            imglist.Images.Clear();            lv_Login.Clear();            lv_Login.Controls.Clear();            lv_Login.View = View.LargeIcon;            //确定大图标的图像列表对象            lv_Login.LargeImageList = imglist;            imglist.ImageSize = new System.Drawing.Size(72, 128);            int result = db.ScalForInt(strCounts);            SqlCommand comm = new SqlCommand(str, db.conn);            //new出对象数组,用于存储目标表格信息            Song[] song = new Song[result];            try            {                db.OpenConnection();                SqlDataReader dr = comm.ExecuteReader();                if (dr != null)                    if (dr.HasRows)                    {                        for (int i = 0; i < result; i++)                        {                            if (dr.Read())                            {                                //读取了歌手id,歌手名称                                song[i] = new Song();                                song[i].SingerId = dr["singer_id"].ToString();                                song[i].SingerName = dr["singer_name"].ToString();                            }                        }                        dr.Close();                    }            }            catch (Exception)            {                MessageBox.Show("私有连接异常!");            }            finally            {                db.CloseConnection();            }
            //改变imagelist的图片大小:imglist.ImageSize = new Size(32, 32);
            //循环读出图片url,加载对应图片到imglist,给items组合项添加对象数组中每一项            for (int i = 0; i < result; i++)            {                song[i].SingerPhoto = GetSingerurlById(song[i].SingerId);                Image img;                //亮点                try                {                    img = Image.FromFile(song[i].SingerPhoto);                }                catch (Exception)                {                    img = Image.FromFile("D:\\课程\\S1\\KTV\\MyKTV\\image\\singer_temp.jpg");                }                imglist.Images.Add(img);                ListViewItem item = new ListViewItem(song[i].SingerName, i);                item.Tag = song[i].SingerId;                lv_Login.Items.Add(item);            }        }        #region 删除播放列表歌曲,双击播放        ////删除选中歌曲按钮事件        private void btnDel_Click(object sender, EventArgs e)        {            int result;            try            {                result = Convert.ToInt32(lvPlay_list.SelectedItems[0].Index);            }            catch (Exception)            {                return;            }            //清空对象数组与列表            lvPlay_list.SelectedItems[0].Remove();            ///亮点            if (result == playingIndex)            {
                try                {                    if (playing_list[result + 1] != null)                    {                        for (int i = result + 1; i < playing_list.Length; i++)                        {                            playing_list[i - 1].SongId = playing_list[i].SongId;                            playing_list[i - 1].SongName = playing_list[i].SongName;                            playing_list[i - 1].Songurl = playing_list[i].Songurl;                        }                        wmp.URL = playing_list[result].Songurl;                    }                    else                    {                        playing_list[result] = playing_list[result + 1];                        wmp.URL = string.Empty;                    }                }                catch (Exception)                {                    wmp.URL = string.Empty;                }            }                   }        ////双击播放列表组件时        private void lvPlay_list_DoubleClick(object sender, EventArgs e)        {            try            {                wmp.URL = playing_list[Convert.ToInt32(lvPlay_list.SelectedItems[0].Index)].Songurl;                lvPlay_list.Items[playingIndex].SubItems[1].Text = "已播放";                playingIndex = Convert.ToInt32(lvPlay_list.SelectedItems[0].Index);                lvPlay_list.Items[playingIndex].SubItems[1].Text = "正在播放";            }            catch (Exception)            {                return;            }        }        ////列表循环方式按钮点击事件        private void btnState_Click(object sender, EventArgs e)        {        //    Timer timeResult = new Timer();        //    if (btnState.Text=="列表循环模式")        //    {        //        btnState.Text = "单曲循环模式";        //        timer.Enabled = false;        //        timeResult.Enabled = true;        //        timeResult.Interval = 1000;        //        timeResult.Tick += timeResult_Tick;        //    }        //    else if (btnState.Text=="单曲循环模式")        //    {        //        btnState.Text = "无循环模式";        //        timer.Enabled = true;        //        timeResult.Enabled = false;        //    }        //    else if (btnState.Text=="无循环模式")        //    {        //        btnState.Text = "列表循环模式";        //        timer.Enabled = false;        //        timeResult.Enabled = true;        //        timeResult.Interval = 1000;        //        timeResult.Tick += timeResult_Tick;        //    }        }        ////附加定时器事件        void timeResult_Tick(object sender, EventArgs e)        {
        //    if (wmp.playState==WMPLib.WMPPlayState.wmppsStopped&&btnState.Text=="单曲循环模式")        //    {        //        wmp.URL = playing_list[playingIndex].Songurl;        //    }        //    else if (btnState.Text=="列表循环模式")        //    {        //        int result=-1;        //        foreach (Song item in playing_list)        //        {        //            if (item!=null)        //            {        //                result++;        //            }        //            else        //            {        //                break;        //            }        //        }        //        if (playingIndex==result)        //        {        //            playingIndex = 0;        //        }        //    }        }        #endregion

 

posted @ 2016-08-05 16:03  积点成线  阅读(350)  评论(0编辑  收藏  举报