第一步:创建数据库连接方法和打开方法和关闭方法!
1 public class DBHelper 2 { 3 private string str = "server=.;database=MyKtv;uid=sa"; 4 private SqlConnection _conection; 5 6 public SqlConnection Conection 7 { 8 get 9 { 10 if (_conection==null) 11 { 12 _conection = new SqlConnection(str); 13 } 14 return _conection; 15 } 16 } 17 /// <summary> 18 /// 打开方法 19 /// </summary> 20 public void OpenConnection() 21 { 22 if (Conection.State == ConnectionState.Closed) 23 { 24 Conection.Open(); 25 } if (Conection.State == ConnectionState.Broken) 26 { 27 Conection.Close(); 28 Conection.Open(); 29 } 30 } 31 /// <summary> 32 /// 关闭方法` 33 /// </summary> 34 public void CloseConnection() 35 { 36 if (Conection.State == ConnectionState.Open && Conection.State == ConnectionState.Broken) 37 { 38 Conection.Close(); 39 } 40 41 }
第二步:歌曲首页
歌曲首页:用到了窗体之间的转换和读取路径表中的图片路径放到filepath上
1 public partial class FormMain : Form 2 { 3 DBHelper db = new DBHelper(); 4 public FormMain() 5 { 6 InitializeComponent(); 7 } 8 private void Form1_Load(object sender, EventArgs e) 9 { 10 //读取路径表中的图片路径放到filepath上 11 string sql = "select resource_path from resource_path where resource_id=1"; 12 SqlCommand cmd = new SqlCommand(sql,db.Conection); 13 db.OpenConnection(); 14 KtvUtil.FilePath = cmd.ExecuteScalar().ToString(); 15 db.CloseConnection(); 16 } 17 18 private void pictureBox1_Click(object sender, EventArgs e) 19 { 20 //打开明星点歌窗体 21 FrmSinger frmSinger = new FrmSinger(); 22 frmSinger.Show(); 23 } 24 25 private void toolStripButton4_Click(object sender, EventArgs e) 26 { 27 this.Close(); 28 } 29 30 private void pictureBox4_Click(object sender, EventArgs e) 31 { 32 //打开拼音点歌窗体 33 FrmOrderBySongName fobsn = new FrmOrderBySongName(); 34 fobsn.ShowDialog(); 35 }
第三步:歌星点歌
歌星点歌:用到了3个ListView之间的跳转,
1 public partial class FrmSinger : Form 2 { 3 DBHelper db = new DBHelper(); 4 public string SingerType = "组合"; 5 public int SingerId = 0; 6 7 public FrmSinger() 8 { 9 InitializeComponent(); 10 } 11 12 private void panel1_Paint(object sender, PaintEventArgs e) 13 { 14 15 } 16 /// <summary> 17 /// 点击歌手类型后加载相应的信息 18 /// </summary> 19 public void ShowSingerDiQu() 20 { 21 if (lvtype.SelectedItems[0]!=null) 22 { 23 lvtype.Visible = false; 24 lvSinger.Visible = true; 25 lvSinger.Location = lvtype.Location; 26 27 28 this.SingerType = Convert.ToString(lvtype.SelectedItems[0].Tag); 29 } 30 string sql = "select singertype_name,singertype_id from singer_type"; 31 SqlCommand cmd = new SqlCommand(sql,db.Conection); 32 SqlDataReader sdr; 33 try 34 { 35 db.OpenConnection(); 36 sdr = cmd.ExecuteReader(); 37 lvtype.Items.Clear(); 38 if (sdr.HasRows) 39 { 40 int result = 0; 41 while (sdr.Read()) 42 { 43 ListViewItem lvitem = new ListViewItem(); 44 string typename = Convert.ToString(sdr["singertype_name"]); 45 int typeid = Convert.ToInt32(sdr["singertype_id"]); 46 lvitem.Text = typename; 47 lvitem.Tag = typeid; 48 lvitem.ImageIndex = result; 49 lvSinger.Items.Add(lvitem); 50 result++; 51 } 52 sdr.Close(); 53 } 54 } 55 catch (Exception ex) 56 { 57 MessageBox.Show("第二个系统报错" + ex.Message); 58 } 59 finally 60 { 61 db.CloseConnection(); 62 } 63 } 64 65 private void listView2_SelectedIndexChanged(object sender, EventArgs e) 66 { 67 68 } 69 70 private void FrmOrderBySinger_Load(object sender, EventArgs e) 71 { 72 this.lvSinger.Visible = false; 73 this.lvContry.Visible=false; 74 } 75 76 private void listView1_Click(object sender, EventArgs e) 77 { 78 ShowSingerDiQu(); 79 } 80 /// <summary> 81 /// 读取对应地区的歌手名称 82 /// </summary> 83 public void ShowSingerName() 84 { 85 if (lvSinger.SelectedItems[0]!=null) 86 { 87 lvSinger.Visible = false; 88 lvContry.Visible = true; 89 lvContry.Location = lvtype.Location; 90 SingerId = Convert.ToInt32(lvSinger.SelectedItems[0].Tag);