S1的小成果:MyKTV系统
转眼之间,已经到了2016年,即新的一年了!S1也结束了,收获的也不多 ,想想最后留给大家的就一个KTV项目了。
希望大家看时有所收获
现在我们一起来看KTV前台管理
主界面的运行效果:
主要代码:
1
2 private void MainForm_Load(object sender, EventArgs e)
3 {
4 // 加载时,运行播放窗体
5 FrmPlay playForm = new FrmPlay();
6 playForm.Show();
7
8 // 启动定时器
9 this.timer1.Start();
10
11 // 读取资源路径
12 DBHelper dbHelper = new DBHelper();
13 string sql = "select resource_path from resource_path where resource_type = 'singer_photo'";
14 SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
15
16 // 读取歌手照片路径
17 try
18 {
19 dbHelper.OpenConnection();
20 KTVUtil.singerPhotoPath = command.ExecuteScalar().ToString();
21 }
22 catch (Exception ex)
23 {
24 MessageBox.Show("资源路径发生错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
25 }
26 finally
27 {
28 dbHelper.CloseConnection();
29 }
30
31 // 读取歌曲路径
32 sql = "select resource_path from resource_path where resource_type = 'song'";
33 command.CommandText = sql;
34 try
35 {
36 dbHelper.OpenConnection();
37 KTVUtil.songPath = command.ExecuteScalar().ToString();
38 }
39 catch (Exception ex)
40 {
41 MessageBox.Show("路径错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
42 }
43 finally
44 {
45 dbHelper.CloseConnection();
46 }
47 }
1 /// <summary>
2 /// 显示当前播放的歌曲名字
3 /// </summary>
4 public void ShowPlayingSongName()
5 {
6 this.lblPlayingSong.Text = PlayList.PlayingSongName();
7 this.lblNextSong.Text = PlayList.NextSongName();
8 }
同一窗体显示不同界面
如果在一个窗体中显示不同的界面呢?
我们可以转换一下思路,所谓界面不同就是容器不同
解决方案:通过控制Form窗体中ListView控件的显示和隐藏来实现多界面窗体
歌星点歌的运行效果:
点击第一个LIstView,弹出第二个ListView解析:1.隐藏第一个ListView,显示第二个ListView 的代码
1 // 点击后,显示歌手类别
2 private void lvOrder_Click(object sender, EventArgs e)
3 {
4 if (lvOrder.SelectedItems[0] != null)
5 {
6 // 隐藏歌手性别,显示歌手类别
7 pnlSingerSex.Visible = false;
8 pnlSingerType.Location = pnlSingerSex.Location;
9 pnlSingerType.Dock = DockStyle.Fill;
10 pnlSingerType.Visible = true;
11 this.singerSex = Convert.ToString(lvOrder.SelectedItems[0].Tag); // 记录选择的性别
12 }
13
14 // 读取歌手类别
15 DBHelper dbHelper = new DBHelper();
16 string sql = "select * from singer_type";
17 try
18 {
19 // 查询数据库
20 SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
21 dbHelper.OpenConnection();//相当于con.open
22 SqlDataReader reader = command.ExecuteReader();
23
24 // 循环将类别读取出来添加到ListView中
25 lvSingerType.Items.Clear();
26 int i = 0;
27 while (reader.Read())
28 {
29 ListViewItem item = new ListViewItem();
30 item.Text = Convert.ToString(reader["singertype_name"]);
31 item.Tag = Convert.ToInt32(reader["singertype_id"]);
32 item.ImageIndex = i;
33 lvSingerType.Items.Add(item);
34 i++;
35 }
36 reader.Close();
37 }
38 catch (Exception ex)
39 {
40 Console.WriteLine(ex.Message);
41 MessageBox.Show("错误!");
42
43 }
44 finally
45 {
46 dbHelper.CloseConnection();
47 }
48 }
需要将【男歌手】汉字传递到第二个ListView上 显示5个国家的信息(包括文本和图片)
需要将【男歌手】汉字传递到第二个ListView上 .显示5个国家的信息(包括文本和图片)
点击第二个ListVIew,填出第三个ListView
效果:
// 点击类别后,显示对应类别下的歌手列表
private void lvSingerType_Click(object sender, EventArgs e)
{
// 隐藏歌手类别,显示歌手列表
pnlSingerType.Visible = false;
pnlSingerList.Location = pnlSingerSex.Location;
pnlSingerList.Dock = DockStyle.Fill;
pnlSingerList.Visible = true;
this.singerTypeId = Convert.ToInt32(lvSingerType.SelectedItems[0].Tag); // 保存选中的类别编号
// 读取数据库,读出歌手信息
DBHelper dbHelper = new DBHelper();
StringBuilder sql = new StringBuilder();
sql.AppendFormat("select singer_id,singer_name,singer_photo_url from singer_info where singertype_id={0} and singer_gender='{1}'",
this.singerTypeId,this.singerSex);
try
{
SqlCommand command = new SqlCommand(sql.ToString(),dbHelper.Connection);
dbHelper.OpenConnection();
SqlDataReader reader = command.ExecuteReader();
int imageIndex = 0; // 代表歌手头像的索引
ilSinger.Images.Clear();
// 循环读出歌手信息添加到窗体中显示
lvSinger.Items.Clear();
while (reader.Read())
{
// 将歌手头像放在ImageList控件中
string photoURL = KTVUtil.singerPhotoPath + "\\" + Convert.ToString(reader["singer_photo_url"]);
ilSinger.Images.Add(Image.FromFile(photoURL));
// 将歌手添加到ListView中
ListViewItem item = new ListViewItem();
item.Text = Convert.ToString(reader["singer_name"]);
item.Tag = Convert.ToString(reader["singer_id"]);
item.ImageIndex = imageIndex;
lvSinger.Items.Add(item);
imageIndex++;
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("错误!");
}
finally
{
dbHelper.CloseConnection();
}
}
第三个ListView出来后,点击其中的一个歌手,弹出该歌手演唱的所有歌曲
效果:
-
1 // 点击歌手姓名,打开歌曲列表窗口 2 private void lvSinger_Click(object sender, EventArgs e) 3 { 4 // 读取数据库,读出该歌手的所有歌曲 5 DBHelper dbHelper = new DBHelper(); 6 StringBuilder sb = new StringBuilder(); 7 sb.AppendFormat("select song_id,song_name, singer_name='{0}',song_url from song_info where singer_id={1}", 8 lvSinger.SelectedItems[0].Text, Convert.ToInt32(lvSinger.SelectedItems[0].Tag)); 9 10 FrmSongList songList = new FrmSongList(); 11 songList.Sql = sb.ToString(); 12 songList.Previous = PrevioisForm.Singer; // 指定返回的窗体是按歌手点歌 13 songList.Show(); 14 this.Close(); 15 }
我们都知道ListView绑定首列的数据是通过
1 ListViewItem lvitem = new ListViewItem(stuno);
2 显示地区图片的代码: SqlDataReader dr = command.ExecuteReader();
3
4
5
6 // 循环将类别读取出来添加到ListView中
7
8 lvlisttwo.Items.Clear();
9
10 int i = 0;
11
12 while (dr.Read())
13
14 {
15
16 ListViewItem item = new ListViewItem();
17
18 item.Text = Convert.ToString(dr["singertype_name"]);
19
20 item.Tag = Convert.ToInt32(dr["singertype_id"]);
21
22 item.ImageIndex = i;
23
24 lvlisttwo.Items.Add(item);
25
26 i++;
27
28 }
29
30 dr.Close();
详细分析:
显示歌手的图片的代码
1 SqlDataReader dr = cmd.ExecuteReader();
2
3 int imageIndex = 0; // 代表歌手头像的索引
4
5 imglistthree.Images.Clear();
8
9 // 循环读出歌手信息添加到窗体中显示
10
11 lvlistthree.Items.Clear();
12
13 while (dr.Read())
14
15 {
16
17 // 将歌手头像放在ImageList控件中
18
19 string photoURL = KTVUtil.singerPhotoPath + "\\" + Convert.ToString(dr["singer_photo_url"]);
20
21 imglistthree.Images.Add(Image.FromFile(photoURL));
24
25 // 将歌手添加到ListView中
26
27 ListViewItem item = new ListViewItem();
28
29 item.Text = Convert.ToString(dr["singer_name"]);
30
31 item.Tag = Convert.ToString(dr["singer_id"]);
32
33 item.ImageIndex = imageIndex;
34
35 lvlistthree.Items.Add(item);
36
37
38
39 imageIndex++;
40
41 }
42
43 dr.Close();
44
45 }
ListView控件首列不能居中问题
我也没找到解决方法,能做的,也许就是把第一列宽度设为 0,不用第一列,从第二列开始用。
此时 ListView1.Items[i].Text 也不能用了,因为它对应的是 ListView1[i].SubItems[0].Text。
设置ToolStrip对应项图片的大小,通过ImageScalingSize来设置。
04.Panel不能实现同一窗体不同界面。
17. 实现播放歌曲功能
01.点击某歌曲后,将选择的歌曲添加到已点列表
02.在已点列表中放入一个Timer控件,实时检测每首歌曲的状态
03.在播放窗口中放入一个Timer控件,实时检测需要播放的歌曲
1 private void PlayForm_Load(object sender, EventArgs e)
2 {
3 this.PlaySong();
4 this.timer1.Start();
5 }
6
7 /// <summary>
8 /// 播放歌曲
9 /// </summary>
10 private void PlaySong()
11 {
12 this.song = PlayList.GetPlayingSong(); // 获取当前要播放的歌曲
13 if (song != null)
14 {
15 this.song.SetSongPlayed(); // 将当前歌曲播放状态设为已播放
16 this.wmpSong.URL = KTVUtil.songPath + "\\" + this.song.SongURL; // 得到当前播放歌曲的路径
17 }
18 }
19
20 private void timer1_Tick(object sender, EventArgs e)
21 {
22 if(this.song == null)
23 {
24 this.PlaySong();
25 }
26 if (this.wmpSong.playState == WMPLib.WMPPlayState.wmppsStopped)
27 {
28 this.song = null; // 将歌曲设为空
29 PlayList.MoveOn();
30 }
31 // 切歌
32 if (this.song != null && this.song.PlayState == SongPlayState.cut)
33 {
34 this.wmpSong.URL = "";
35 this.song = null;
36 }
37 }
实现播放列表操作:
1.KTVUtil 类:
1 class KTVUtil
2
3 {
4
5 public static string singerPhotoPath = ""; // 歌手照片路径
6
7 public static string songPath = ""; // 歌曲路径
8
9 }
2.编写歌曲类(Song)
1 代码例子:enum SongPlayState
2
3 {
4
5 unplayed, played, again, cut
6
7 }
8
9 // 歌曲类
10
11 class Song
12
13 {
14
15 //歌曲名称
16
17 public string SongName
18
19 {
20
21 get { return songName; }
22
23 set { songName = value; }
24
25 }
26
27 //歌曲存放路径
28
29 public string SongURL
30
31 {
32
33 get { return songURL; }
34
35 set { songURL = value; }
36
37 }
38
39 // 歌曲播放状态
40
41 internal SongPlayState PlayState
42
43 {
44
45 get { return playState; }
46
47 set { playState = value; }
48
49 }
50
51 private string songName;
52
53 private string songURL;
54
55 // 歌曲播放状态,默认为未播放状态
56
57 private SongPlayState playState = SongPlayState.unplayed;
58
59 //将歌曲状态改为已播放
60
61 public void SetSongPlayed()
62
63 {
64
65 this.playState = SongPlayState.played;
66
67 }
68
69 // 将歌曲状态改为再拨放一次
70
71 public void SetPlayAgain()
72
73 {
74
75 this.playState = SongPlayState.again;
76
77 }
78
79 // 将歌曲状态改为切歌
80
81 public void SetSongCut()
82
83 {
84
85 this.playState = SongPlayState.cut;
3.编写播放列表类(PlayList),提供播放列表的各种方法
代码例子:// 播放列表管理
1
2 class PlayList
3
4 {
5
6 // 歌曲播放列表数组
7
8 private static Song[] songList = new Song[50];
9
10 // 当前播放的歌曲在数组中的索引
11
12 private static int songIndex = 0;
13
14 // 播放列表数组
15
16 public static Song[] SongList
17
18 {
19
20 get { return PlayList.songList; }
21
22 }
23
24 //当前播放歌曲的索引
25
26 public static int SongIndex
27
28 {
29
30 get { return PlayList.songIndex; }
31
32 }
33
34 // 当前播放的歌曲名称
35
36 public static string PlayingSongName()
37
38 {
39
40 string songName = ""; // 歌曲名称
41
42 if (SongList[SongIndex] != null)
43
44 {
45
46 songName = SongList[SongIndex].SongName;
47
48 }
49
50 return songName;
51
52 }
53
54 //获取当前播放的歌曲
55
56 public static Song GetPlayingSong()
57
58 {
59
60 if (SongList[songIndex] != null)
61
62 {
63 return SongList[songIndex];
64 }
65
66 else
67
68 {
69 return null;
70
71 }
72
73 }
74
75 //下一首要播放的歌曲名称
76
77 public static string NextSongName()
78
79 {
80
81 string songName = ""; // 歌曲名称
82
83 if (SongList[SongIndex + 1] != null)
84
85 {
86 songName = SongList[SongIndex + 1].SongName;
87 }
88 return songName;
89
90 }
91
92
93
94 // 点播,及添加播放,一首歌曲
95
96 public static bool AddSong(Song song)
97
98 {
99
100 //默认为没有添加播放歌曲
101
102 bool success = false;
103
104 //for遍历Song[],
105
106 for (int i = 0; i < SongList.Length; i++)
107 {
108 if (SongList[i] == null)
109
110 {
111
112 SongList[i] = song;
113
114 // Console.WriteLine(song.SongName);
115
116 //返回要播放的歌曲,
117
118 success = true;
119
120 break;
121 }
122 }
123 return success;
124
125 }
126 // 切歌 要切歌曲的编号,如果是切当前播放的歌曲传入-1
127
128 public static void CutSong(int index)
129
130 {
131
132 int i; // 循环变量,代表切歌的位置
133
134 if (index == -1)
135
136 {
137 i = SongIndex;
138 }
139 else
140 {
141 i = index; // 从切歌的位置开始,将歌曲逐个向前移一个位置
142 }
143 SongList[i].SetSongCut();
144 while (SongList[i] != null)
145 {
146 SongList[i] = SongList[i + 1];
147 i++;
148 // 如果到达数组最后一个元素,就将最后一个元素指向空
149 if (i == SongList.Length)
150 {
151 SongList[i] = null;
152 }
153
154 }
155
156 }
157 // 重放当前歌曲
158 public static void PlayAgain()
159 {
160 if (SongList[songIndex] != null)
161 {
162 SongList[songIndex].SetPlayAgain();
163 }
164 }
165 // 播放下一首
166 public static void MoveOn()
167
168 {
169 if (SongList[songIndex] != null && SongList[songIndex].PlayState == SongPlayState.again)
170 {
171 SongList[songIndex].SetSongPlayed();
172 }
173 else
174 {
175 songIndex++;
176
177 } }
去Time控件:在Tick事件中写代码
代码例子: // 定时扫描歌曲列表,显示当前播放歌曲的名称
1 private void PlaySong()
2
3 {
4 this.song = PlayList.GetPlayingSong(); // 获取当前要播放的歌曲
5
6 if (song != null)
7 {
8
9 this.song.SetSongPlayed(); // 将当前歌曲播放状态设为已播放
10 this.Winplaymedia.URL = KTVUtil.songPath + "\\" + this.song.SongURL; // 得到当前播放歌曲的路径
11
12 }
13
14 }
15 private void timer1_Tick(object sender, EventArgs e)
16
17 {
18
19 // 在文本框中显示当前播放的歌曲名字
20
21 this.txtplay.Text = PlayList.PlayingSongName();
22
23 this.txtnextsong.Text = PlayList.NextSongName();
24
25 if (this.song == null)
26
27 {
28 this.PlaySong();
29 }
30
31 if (this.Winplaymedia.playState == WMPLib.WMPPlayState.wmppsStopped)
32
33 {
34
35 this.song = null; // 将歌曲设为空
36
37 PlayList.MoveOn();
38
39 }
40
41 // 切歌
42
43 if (this.song != null && this.song.PlayState == SongPlayState.cut)
44
45 {
46
47 this.Winplaymedia.URL = "";
48
49 this.song = null;
50
51 }
在Load时中启动计时器:
代码例子:
this.time.Start();
01:切歌:
1 代码例子: DialogResult re = MessageBox.Show("确定要切歌吗?", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
2
3 if (re == DialogResult.OK)
4
5 {
6
7 PlayList.CutSong(-1);
8
9 }
02:重唱:
代码例子:
1 PlayList.PlayAgain();
2
3 PlaySong();
4
5
移动窗体的代码:
1 private Point mouseOffset; //记录鼠标指针的坐标
2
3 private bool isMouseDown = false; //记录鼠标按键是否按下
4
5 private void pnlon_MouseDown(object sender, MouseEventArgs e)
6
7 {
8
9 int xOffset;
10
11 int yOffset;
12
13 if (e.Button == MouseButtons.Left)
14
15 {
16 xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
17
18 yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
19
20 mouseOffset = new Point(xOffset, yOffset);
21
22 isMouseDown = true;
23
24 }
25
26 }
27 private void pnlon_MouseMove(object sender, MouseEventArgs e)
28
29 {
30 if (isMouseDown)
31
32 {
33 Point mousePos = Control.MousePosition;
34
35 mousePos.Offset(mouseOffset.X + 5, mouseOffset.Y + 30);
36 Location = mousePos;
37
38 }
39
40 }
41
42
43
44 private void pnlon_MouseUp(object sender, MouseEventArgs e)
45
46 {
47 // 修改鼠标状态isMouseDown的值
48
49 // 确保只有鼠标左键按下并移动时,才移动窗体
50
51 if (e.Button == MouseButtons.Left)
52
53 {
54
55 isMouseDown = false;
56
57 }
58
59 }
60
拼音点歌
1 // 查询歌曲显示在窗体中
2 private void btnSearch_Click(object sender, EventArgs e)
3 {
4 DBHelper dbHelper = new DBHelper();
5 DataSet dataSet = new DataSet();
6 StringBuilder sb = new StringBuilder();
7 sb.Append("select song_id,song_name,singer_name,song_url from song_info inner join singer_info on singer_info.singer_id=song_info.singer_id ");
8 sb.AppendFormat("where song_name like '%{0}%' or song_ab like '{0}'",this.txtSongName.Text);
9
10 Console.WriteLine(sb.ToString());
11
12 SqlDataAdapter adapter = new SqlDataAdapter(sb.ToString(), dbHelper.Connection);
13
14 // 清空当前列表
15 if (dataSet.Tables["songList"] != null)
16 {
17 dataSet.Tables["songList"].Clear();
18 }
19
20 adapter.Fill(dataSet, "songList");
21 this.dgvSong.DataSource = dataSet.Tables["songList"];
22 }
23
24 private void tsbtnExit_Click(object sender, EventArgs e)
25 {
26 this.Close();
27 }
28
29 private void dgvSong_CellClick(object sender, DataGridViewCellEventArgs e)
30 {
31 if (dgvSong.SelectedRows[0].Cells["songName"]!=null)
32 {
33 // 创建一个歌曲对象,并将当权选中的歌曲名和路径赋给该对象
34 Song song = new Song();
35 song.SongName = dgvSong.SelectedRows[0].Cells["songName"].Value.ToString();
36 song.SongURL = dgvSong.SelectedRows[0].Cells["songURL"].Value.ToString();
37 PlayList.AddSong(song);
38
39 // 更新数据库,将选中的歌曲点播次数加1
40 int songId = Convert.ToInt32(dgvSong.SelectedRows[0].Cells["songId"].Value);
41 string sql = string.Format("update song_info set song_play_count=song_play_count+1 where song_id={0}", songId);
42 DBHelper dbHelper = new DBHelper();
43 try
44 {
45 SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
46 dbHelper.OpenConnection();
47 command.ExecuteNonQuery();
48 }
49 catch (Exception ex)
50 {
51 Console.WriteLine(ex.Message);
52 MessageBox.Show("错误!");
53 }
54 finally
55 {
56 dbHelper.CloseConnection();
57 }
58 }
59 }
实现分类点歌功能
1 // 窗体加载时,显示歌曲类别
2 private void OrderBySongTypeForm_Load(object sender, EventArgs e)
3 {
4 // 读取歌曲类别
5 DBHelper dbHelper = new DBHelper();
6 string sql = "select * from song_type";
7 try
8 {
9 // 查询数据库
10 SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
11 dbHelper.OpenConnection();
12 SqlDataReader reader = command.ExecuteReader();
13
14 // 循环将类别读取出来添加到ListView中
15 this.lvSongType.Items.Clear();
16 int i = 0;
17 while (reader.Read())
18 {
19 ListViewItem item = new ListViewItem();
20 item.Text = Convert.ToString(reader["songtype_name"]);
21 item.Tag = Convert.ToInt32(reader["songtype_id"]);
22 item.ImageIndex = i;
23 this.lvSongType.Items.Add(item);
24 i++;
25 }
26 reader.Close();
27 }
28 catch (Exception ex)
29 {
30 Console.WriteLine(ex.Message);
31 MessageBox.Show("cuowu!");
32
33 }
34 finally
35 {
36 dbHelper.CloseConnection();
37 }
38 }
39
40 private void lvSongType_Click(object sender, EventArgs e)
41 {
42 // 读取数据库,读出该歌手的所有歌曲
43 DBHelper dbHelper = new DBHelper();
44 StringBuilder sb = new StringBuilder();
45 sb.Append("select song_id,song_name, singer_name,song_url from song_info inner join singer_info on song_info.singer_id=singer_info.singer_id ");
46 sb.AppendFormat("where songtype_id={0}",Convert.ToInt32(lvSongType.SelectedItems[0].Tag));
47 Console.WriteLine(sb.ToString());
48 FrmSongList songList = new FrmSongList();
49 songList.Sql = sb.ToString();
50 songList.Previous = PrevioisForm.SongType;
51 songList.Show();
52 this.Close();
53 }
字数点歌
1 private void OrderByWordCountForm_Load(object sender, EventArgs e)
2 {
3 // 将字数列表添加到窗体中
4 for (int i = 0; i < 12; i++)
5 {
6 // 循环生成字数项添加到窗体中
7 ListViewItem item = new ListViewItem();
8 item.Text = (i + 1)+"个字";
9 item.Tag = i + 1;
10 lvWordCount.Items.Add(item);
11 }
12
13 }
14
15 private void lvWordCount_Click(object sender, EventArgs e)
16 {
17 if (lvWordCount.SelectedItems[0] != null)
18 {
19 // 读取数据库,读出该歌手的所有歌曲
20 DBHelper dbHelper = new DBHelper();
21 StringBuilder sb = new StringBuilder();
22 sb.Append("select song_id,song_name, singer_name,song_url from song_info inner join singer_info on song_info.singer_id=singer_info.singer_id ");
23 sb.AppendFormat("where song_word_count={0}", Convert.ToInt32(lvWordCount.SelectedItems[0].Tag));
24 Console.WriteLine(sb.ToString());
25 FrmSongList songList = new FrmSongList();
26 songList.Sql = sb.ToString();
27 songList.Previous = PrevioisForm.WordCount;
28 songList.Show();
29 this.Close();
30 }
31 }
已点歌曲:
1 //刷新歌曲列表
2
3 private void timer1_Tick(object sender, EventArgs e){
4
5 this.newSonglist();
6
7 }
8
9 private void newSonglist()
10
11 {
12
13 // 清空原列表
14
15 lvlist.Items.Clear();
16
17 int index = 0;
18
19 while (PlayList.SongList[index] != null)
20
21 {
22
23 ListViewItem item = new ListViewItem();
24
25 //获取歌曲的名称
26
27 item.Text = PlayList.SongList[index].SongName;
28
29 item.Tag = index;
30
31 //歌曲的播放状态
32
33 string playState = PlayList.SongList[index].PlayState == SongPlayState.unplayed ? "未播放" : "已播放";
34
35 item.SubItems.Add(playState);
36
37 lvlist.Items.Add(item);
38
39 index++;
40
41 }
42
43 }
44
45 //切割
46
47 private void tsbtnCut_Click_1(object sender, EventArgs e)
48
49 {
50
51 int songId = -1; // 切歌的编号
52
53 DialogResult re = MessageBox.Show("确定要切歌吗?", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
54
55 if (re == DialogResult.OK)
57 {
58
59 if (this.lvlist.SelectedItems.Count > 0)
60
61 {
62
63 songId = Convert.ToInt32(this.lvlist.SelectedItems[0].Tag);
64
65 }
66
67 PlayList.CutSong(songId);
68
69 this.newSonglist();
70
71 }
72
73
歌曲列表:
1 // 窗体加载时查询歌曲列表
2 private void SongListForm_Load(object sender, EventArgs e)
3 {
4 DBHelper dbHelper = new DBHelper();
5 DataSet dataSet = new DataSet();
6 SqlDataAdapter adapter = new SqlDataAdapter(this.Sql,dbHelper.Connection);
7 adapter.Fill(dataSet, "songList");
8 dgvSong.DataSource = dataSet.Tables["songList"];
9 }
10
11 // 点播一首歌曲
12 private void dgvSong_CellClick(object sender, DataGridViewCellEventArgs e)
13 {
14 // 创建一个歌曲对象,并将当权选中的歌曲名和路径赋给该对象
15 Song song = new Song();
16 song.SongName = dgvSong.SelectedRows[0].Cells["songName"].Value.ToString();
17 song.SongURL = dgvSong.SelectedRows[0].Cells["songURL"].Value.ToString();
18 PlayList.AddSong(song);
19
20 // 更新数据库,将选中的歌曲点播次数加1
21 int songId = Convert.ToInt32(dgvSong.SelectedRows[0].Cells["songId"].Value);
22 string sql = string.Format("update song_info set song_play_count=song_play_count+1 where song_id={0}", songId);
23 DBHelper dbHelper = new DBHelper();
24 try
25 {
26 SqlCommand command = new SqlCommand(sql, dbHelper.Connection);
27 dbHelper.OpenConnection();
28 command.ExecuteNonQuery();
29 }
30 catch (Exception ex)
31 {
32 Console.WriteLine(ex.Message);
33 MessageBox.Show("错误!");
34 }
35 finally
36 {
37 dbHelper.CloseConnection();
38 }
39 }
后台管理
KTVUtil 类:
1 public class KTVUtil
2
3 {
4
5 //保存歌曲的目录
6
7 public static string songURL = "";
8
9 //保存歌手图片的目录
10
11 public static string singer_photoURL = "";
12
13 }
主窗体的Load事件:
1 private void frmKTV_Load(object sender, EventArgs e)
2
3 {
4
5
6
7 // 歌曲路径
8
9 string sql = "select resource_path from Resource_path where resource_id=2";
10
11 KTVUtil.songURL = song_path(sql);
12
13 // 歌手图片路径
14
15 string sql1 = "select resource_path from Resource_path where resource_id=1";
16
17 KTVUtil.singer_photoURL = song_path(sql1);
18
19 }
20
21 //路径
22
23 private string song_path(string sql)
24
25 {
26
27 SqlConnection con = new SqlConnection(DBHelp.str);
28
29 SqlCommand cmd = new SqlCommand(sql, con);
30
31 string path = "";
32
33
34
35 con.Open();
36
37 SqlDataReader dr = cmd.ExecuteReader();
38
39
40
41 if (dr != null)
42
43 {
44
45 if (dr.HasRows)
46
47 {
48
49 while (dr.Read())
50
51 {
52
53 path = dr["resource_path"].ToString();
增加修改歌手信息:
1 获取图片的代码:
2
3 //相对路径
4
5 public string FileName;
6
7 //绝对路径
8
9 public string Path;
10
11 //获取
12
13 private void btnLiu_Click(object sender, EventArgs e)
14
15 {
16
17 DialogResult result = open.ShowDialog();
18
19
20
21 if (result == DialogResult.OK)//获取路径
22
23 {
24
25 //相对路径
26
27 FileName = open.SafeFileName;
28
29 //绝对路径
30
31 Path = open.FileName;
32
33 pic.Image = Image.FromFile(Path);
39 }
40
41 }
42
43 //复制图片的路径
44
45 if (Path != null)
46
47 {
48
49 if (Path != KTVUtil.singer_photoURL + FileName)
50
51 {
52
53 File.Copy(Path, KTVUtil.singer_photoURL + FileName, true);
54
55 }
56
57 }
58
59
60
61 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
62
63 {
64
65 if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "")
66
67 {
68
69 //获取选中行的歌曲编号
70
71 int songid = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_id"].Value);
72
73 FrmAddSong frm = new FrmAddSong();
74
75 frm.songid = songid;
76
77 frm.Show();
78
79 }
80
81 else
82
83 {
84
85 MessageBox.Show("选歌曲");
86
87 }
查询,删除,歌手信息
1 public void deletesonginfo()
2 {
3 //获取选中行的歌手编号
4 singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["siigid"].Value);
5 string sql = "delete song_info where singer_id=" + singer_id + "";
6
7 SqlCommand cmd = new SqlCommand(sql, con);
8 con.Open();
9 cmd.ExecuteNonQuery();
10 con.Close();
11
12 }
13 //删除歌手
14 public void deletesingerinfo()
15 {
16 //获取选中行的歌手编号
17 singer_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["siigid"].Value);
18 string sql1 = "select song_id from song_info where singer_id=" + singer_id + "";
19 SqlConnection con1 = new SqlConnection(DBHelp.str);
20 SqlCommand cmd1 = new SqlCommand(sql1, con1);
21 con1.Open();
22 int song_id = Convert.ToInt32(cmd1.ExecuteScalar());
23 con1.Close();
24 if (song_id != 0)
25 {
26 MessageBox.Show("删除该歌手的歌曲信息");
27
28 }
29 else
30 {
31 string sql = "delete dbo.singer_info where singer_id=" + singer_id + " ";
32
33 SqlCommand cmd = new SqlCommand(sql, con);
34 con.Open();
35 int count = cmd.ExecuteNonQuery();
36 if (count > 0)
37 {
38 MessageBox.Show("成功!");
39 string RowFilter = "";
40 SelectInfo(RowFilter);
41 }
42 else
43 {
44 MessageBox.Show("失败!");
45 }
46
47 con.Close();
48
49 }
50
51 }
52 //给dgvlist控件绑定数据的方法
53 public void SelectInfo(string RowFilter)
54 {
55 dgvlist.AutoGenerateColumns = false;
56 //歌手姓名,歌手类型,歌手性别,歌手描述
57
58 string sql = "select singer_info.singertype_id,singer_name,singer_id,singertype_name,singer_gender,singer_description from dbo.singer_info,dbo.singer_type where singer_info.singertype_id=singer_type.singertype_id ";
59 SqlDataAdapter da = new SqlDataAdapter(sql, con);
60 DataSet ds = new DataSet();
61 try
62 {
63 da.Fill(ds, "info");
64 DataView dv = new DataView(ds.Tables["info"]);
65 dv.RowFilter = RowFilter;
66 dgvlist.DataSource = dv;
67 }
68 catch (Exception)
69 {
70
71 MessageBox.Show("网络异常!");
72 }
73 finally
74 {
75 con.Close();
76 }
77 }
78 private void btnselect_Click(object sender, EventArgs e)
79 {
80
81 if (txtname.Text == "" && cboty.Text == "全部")
82 {
83 string RowFilter = "";
84 SelectInfo(RowFilter);
85 }
86 else if (txtname.Text != "")
87 {
88 //获取要查询歌手的姓名
89 name = txtname.Text;
90 //获取要查询的歌手类型
91 int type = Convert.ToInt32(cboty.SelectedValue);
92 string RowFilter = "singer_name like '%" + name + "%'";
93 SelectInfo(RowFilter);
94
95 }
96 else
97 {
98
99 //获取要查询歌手的姓名
100 name = txtname.Text;
101 //获取要查询的歌手类型
102 int type = Convert.ToInt32(cboty.SelectedValue);
103 string RowFilter = "singer_name like '%" + name + "%' and singertype_id=" + type + "";
104 SelectInfo(RowFilter);
105 }
106
107 }
108 //给歌手类型下拉框绑定数据的方法
109 public void LoadINGO()
110 {
111
112 string sql = "select * from dbo.singer_type";
113 SqlDataAdapter da = new SqlDataAdapter(sql, con);
114 DataSet ds = new DataSet();
115 try
116 {
117 da.Fill(ds, "info");
118 cboty.DataSource = ds.Tables["info"];
119 cboty.DisplayMember = "singertype_name";
120 cboty.ValueMember = "singertype_id";
121 DataRow row = ds.Tables["info"].NewRow();
122 row["singertype_id"] = -1;
123 row["singertype_name"] = "全部";
124 ds.Tables["info"].Rows.InsertAt(row, 0);
125 cboty.SelectedIndex = 0;
126
127
128 }
129 catch (Exception)
130 {
131
132 MessageBox.Show("网路异常!");
133 }
134 finally
135 {
136 con.Close();
137 }
138 }
139 private void FrmSelectSinger_Load(object sender, EventArgs e)
140 {
141
142 // 给歌手类型下拉框绑定数据的方法
143 LoadINGO();
144 //给dgvlist控件绑定数据
145 string RowFilter = "";
146 SelectInfo(RowFilter);
147 }
148
149 private void 删除ToolStripMenuItem_Click_1(object sender, EventArgs e)
150 {
151 if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "")
152 {
153 DialogResult result = MessageBox.Show("确定删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
154 if (result == DialogResult.OK)
155 {
156 //删除歌手信息的方法
157 deletesonginfo();
158 deletesingerinfo();
159 string RowFilter = "";
160 SelectInfo(RowFilter);
增加歌曲和修改歌曲:
1 public int songid;
2
3 public string Path;
4
5 //相对路径
6
7 public string txtxdpath;
8
9 //新增歌曲信息的方法
10
11 public void addinfo()
12
13 {
14
15 //获取歌曲名称
16
17 string song_name = txtname.Text;
18
19 //获取拼音缩写信息
20
21 string py = txtsuo.Text;
22
23 //获取歌曲类型对应的隐藏值
24
25 int type_id = Convert.ToInt32(cboty.SelectedValue);
26
27 //获取歌手姓名对应的编号
28
29 int singe_id = Convert.ToInt32(txtsername.Tag);
30
31 //获取歌曲文件名
32
33 string url = txtsongurl.Text;
34
35 //获取歌曲名称的长度
36
37 int length = song_name.Length;
38
39 //拼接sql语句
40
41 string sql = "insert into song_info values('" + song_name + "','" + py + "'," + length + "," + type_id + "," + singe_id + ",'" + url + "',default)";
42
43 SqlCommand cmd = new SqlCommand(sql, con);
44
45 con.Open();
46
47 int result = cmd.ExecuteNonQuery();
48
49 if (result > 0)
50
51 {
52
53 MessageBox.Show("保存成功!");
54
55 if (Path != null)
56
57 {
58
59 if (Path != KTVUtil.songURL + txtxdpath)
60
61 {
62
63 File.Copy(Path, KTVUtil.songURL + txtxdpath, true);
64
65 }
66 }
67
68 }
69
70 else
71
72 {
查询,删除歌曲信息
1 //删除歌曲信息的方法
2 public void deletesong()
3 {
4
5 if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "")
6 {
7 //获取选中行的歌曲编号
8 int song_id = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_id"].Value);
9 DialogResult result = MessageBox.Show("删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
10 if (result == DialogResult.OK)
11 {
12 SqlConnection con = new SqlConnection(DBHelp.str);
13 string sql = "delete song_info where song_id=" + song_id + "";
14 SqlCommand cmd = new SqlCommand(sql, con);
15
16 con.Open();
17 int count = cmd.ExecuteNonQuery();
18 if (count > 0)
19 {
20 MessageBox.Show("成功!");
21 string RowFilter="";
22 LoadAllINFO(RowFilter);
23 }
24 else
25 {
26 MessageBox.Show("失败!");
27 }
28
29 con.Close();
30
31 }
32
33 }
34 else
35 {
36
37 MessageBox.Show("选歌曲");
38
39 }
40 }
41 //给歌曲类型下拉框赋值的方法
42 public void LoadINFO()
43 {
44
45 SqlConnection con = new SqlConnection(DBHelp.str);
46 string sql = "select * from song_type";
47 SqlDataAdapter da = new SqlDataAdapter(sql, con);
48 DataSet ds = new DataSet();
49
50 da.Fill(ds, "info");
51 cbolist.DataSource = ds.Tables["info"];
52 cbolist.DisplayMember = "songtype_name";
53 cbolist.ValueMember = "songtype_id";
54
55 DataRow row = ds.Tables["info"].NewRow();
56 row["songtype_id"] = -1;
57 row["songtype_name"] = "全部";
58 ds.Tables["info"].Rows.InsertAt(row, 0);
59 cbolist.SelectedIndex = 0;
60
61
62
63
64 con.Close();
65
66
67 }
68
69 //给dgvlist绑定数据的方法
70 public void LoadAllINFO(string RowFilter)
71 {
72 //取消英文列自动生成
73 dgvlist.AutoGenerateColumns = false;
74 SqlConnection con = new SqlConnection(DBHelp.str);
75 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";
76 SqlDataAdapter da = new SqlDataAdapter(sql, con);
77 DataSet ds = new DataSet();
78
79 da.Fill(ds, "info");
80 DataView dv = new DataView(ds.Tables["info"]);
81 dv.RowFilter = RowFilter;
82 dgvlist.DataSource = dv;
83
84 con.Open();
85
86
87 }
88 private void btnselect_Click(object sender, EventArgs e)
89 {
90
91
92 if (txtname.Text == "" && cbolist.Text == "全部")
93 {
94 string RowFilter = "";
95 LoadAllINFO(RowFilter);
96 }
97 else if (txtname.Text != "")
98 {
99 //获取歌曲名称
100 string song_name = txtname.Text;
101 //获取歌曲类型对应的隐藏值
102 int type_id = Convert.ToInt32(cbolist.SelectedValue);
103 string RowFilter = "song_name like '%" + song_name + "%'";
104 LoadAllINFO(RowFilter);
105
106 }
107 else
108 {
109
110 //获取歌曲名称
111 string song_name = txtname.Text;
112 //获取歌曲类型对应的隐藏值
113 int type_id = Convert.ToInt32(cbolist.SelectedValue);
114 string RowFilter = "song_name like '%" + song_name + "%' and songtype_id =" + type_id + "";
115 LoadAllINFO(RowFilter);
116
117 }
118 }
119
120 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
121 {
122 if (dgvlist.SelectedRows[0].Cells[0].Value.ToString() != "")
123 {
124 //获取选中行的歌曲编号
125 int songid = Convert.ToInt32(dgvlist.SelectedRows[0].Cells["song_id"].Value);
126 FrmAddSong frm = new FrmAddSong();
127 frm.songid = songid;
128 frm.Show();
129 }
130 else
131 {
132 MessageBox.Show("选歌曲");
133 }
134 }
135
136 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
137 {
138
139 //删除歌曲信息的方法
140 deletesong();
141 }
142
143 private void frmselectSong_Load(object sender, EventArgs e)
144 {
145 //给歌曲类型下拉框赋值
146 LoadINFO();
147 //给dgvlist绑定数据
148 string RowFilter = "";
149 LoadAllINFO(RowFilter);
150 }
歌曲路径:
1 private void frmAddSongURL_Load(object sender, EventArgs e)
2
3 {
4
5 string sql = "select resource_path from Resource_path where resource_id=2";
6
7 txtoldurl.Text = song_path(sql);
8
9 }
10
11 //给当前路径赋值
12
13 private string song_path(string sql)
14
15 {
16
17
18
19 SqlCommand cmd = new SqlCommand(sql, con);
20
21 string path = "";
22
23 try
24
25 {
26
27 con.Open();
28
29 SqlDataReader dr = cmd.ExecuteReader();
30
31
32
33 if (dr != null)
34
35 {
36
37 if (dr.HasRows)
38
39 {
40
41 while (dr.Read())
42
43 {
44
45 path = dr["resource_path"].ToString();
46
47 }
48
49
50
51 DialogResult re = MessageBox.Show("修改路径?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
52
53 if (re == DialogResult.Yes)
54
55 {
56
57
58
59 Directory.Delete(txtnewurl.Text);
60
61 Directory.Move(txtoldurl.Text, txtnewurl.Text);
62
63 string newUrl = txtnewurl.Text;
64
65 SqlConnection con = new SqlConnection(DBHelp.str);
66
67 string sql = "update Resource_path set resource_path='" + newUrl + "' where resource_type='song'";
68
69 SqlCommand cmd = new SqlCommand(sql, con);
70
71 try
72
73 {
74
75 con.Open();
76
77 int count = cmd.ExecuteNonQuery();
78
79 if (count > 0)
80
81 //浏览新路径
82
83 private void btnLiu_Click(object sender, EventArgs e)
84
85 {
86
87
88
89 DialogResult result = brow.ShowDialog();
90
91 if (result == DialogResult.OK)
92
93 {
94
95 txtnewurl.Text = brow.SelectedPath + "\\";
96
97 }
1 //歌曲的文件名:
2
3 //浏览
4
5 private void btnLiu_Click(object sender, EventArgs e)
6
7 {
8
9 open.Filter = "歌曲文件|*.mp3;";
10
11 DialogResult result = open.ShowDialog();
12
13 if (result == DialogResult.OK)//获取路径
14
15 {
16
17 //相对路径
18
19 txtxdpath = open.SafeFileName;
20
21 txtsongurl.Text = txtxdpath;
22
23 //绝对路径
24
25 Path = open.FileName;
26
27
28
29 int dot = txtxdpath.LastIndexOf('.');
30
31 string fileType = txtxdpath.Substring(dot + 1);
32
33 if (fileType != "mp3")
34
35 {
36
37 MessageBox.Show("文件类型不对!");
歌手图片的路径:
1 //给当前路径赋值
2
3 private string song_path(string sql)
4
5 {
6
7 SqlConnection con = new SqlConnection(DBHelp.str);
8
9 SqlCommand cmd = new SqlCommand(sql, con);
10
11 string path = "";
12
13
14
15 con.Open();
16
17 SqlDataReader dr = cmd.ExecuteReader();
18
19
20
21 if (dr != null)
22
23 {
24
25 if (dr.HasRows)
26
27 {
28
29 while (dr.Read())
30
31 {
32
33 path = dr["resource_path"].ToString();
34
35 }
36
37 private void btnSa_Click(object sender, EventArgs e)
38
39 {
40
41 // 如果新路径为空,提示
42
43 if (this.txtnewurl.Text.Trim() == "")
44
45 {
46
47 MessageBox.Show("请选择新路径!");
48
49 }
50
51 else
52
53 {
54
55 // 用户确认修改
56
57 if (MessageBox.Show("确定要修改路径吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
58
59 {
60
61
62
63 Directory.Delete(txtnewurl.Text);
64
65 Directory.Move(txtoldurl.Text, txtnewurl.Text);
66
67 string newUrl = txtnewurl.Text;
68
69 SqlConnection con = new SqlConnection(DBHelp.str);
70
71 string sql = "update Resource_path set resource_path='" + newUrl + "' where resource_type='singer_photo'";
72
73 SqlCommand cmd = new SqlCommand(sql, con);
74
75 con.Open();
76
77 int count = cmd.ExecuteNonQuery();
78
79 if (count > 0)
80
81 {
82
83 //浏览
84
85 private void btnLiu_Click(object sender, EventArgs e)
86
87 {
88
89 DialogResult result = brow.ShowDialog();
90
91 if (result == DialogResult.OK)
92
93 {
94
95 txtnewurl.Text = brow.SelectedPath + "\\";
96
97 }
汉字转拼音首字母
1 public static string GetChineseSpell(string strText)
2 {
3 if (strText == null || strText.Length == 0)
4 return strText;
5 System.Text.StringBuilder myStr = new System.Text.StringBuilder();
6 foreach (char vChar in strText)
7 {
8 // 若是字母则直接输出
9 if ((vChar >= 'a' && vChar <= 'z') || (vChar >= 'A' && vChar <= 'Z'))
10 myStr.Append(char.ToUpper(vChar));
11 else if ((int)vChar >= 19968 && (int)vChar <= 40869)
12 {
13 // 若字符Unicode编码在编码范围则 查汉字列表进行转换输出
14 foreach (string strList in strChineseCharList)
15 {
16 if (strList.IndexOf(vChar) > 0)
17 {
18 myStr.Append(strList[0]);
19 break;
20 }
21 }
22 }
23 }
24 return myStr.ToString();
25 }// GetChineseSpell
26
27
这可是我辛辛苦苦的成果哦,虽然这不算什么,但我还希望大家看看就能收获一些知识,就开心了。