求这段代码的问题,我想实现图片循环轮换。
现在里面有2条记录,第一条的cj_names是"耐克",第二条的cj_names是"阿迪"。
现在这段代码的问题是:
1、图片不循环,显示两张之后就停止了。
2、label的名字第一张停止的显示 “阿”,第二张停止的显示“迪”。应该是第一张停止显示“耐克”,第二张显示“阿迪”。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string sql_str = "select * from cj_main";
            SqlConnection conn = new SqlConnection(conn_str);
            SqlCommand command = new SqlCommand(sql_str, conn);
            conn.Open();
            SqlDataReader rs = command.ExecuteReader();
        }
        string conn_str = "Server=WIN_TEST;Database=choujiang;uid=sa;pwd=dsd_dsdn";
        string cj_names;
        string cj_pic_path;
        int i = 0;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                timer1.Enabled = true;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = true;
            }
            if (e.KeyCode == Keys.Enter)
            {
                timer1.Enabled = false;
                label1.Visible = true;
                label3.Visible = false; 
            }
            if (e.KeyData == Keys.Escape) Application.Exit();
            {
            }
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            string sql_str = "select * from cj_main";
            SqlConnection conn = new SqlConnection(conn_str);
            SqlCommand command = new SqlCommand(sql_str, conn);
            conn.Open();
            SqlDataReader rs = command.ExecuteReader();
            List<Image> lst = new List<Image>();
            List<string>names = new List<string>();
            while(rs.Read())
            {
                cj_names = rs[1].ToString();
                cj_pic_path = rs[3].ToString();
                lst.Add(Image.FromFile(cj_pic_path));
                names.Add("恭喜您抽中了:"+cj_names);
                if (i >= lst.Count)
                {
                    i = 0;
                }
                else
                {
                    this.pictureBox1.Image = lst[i];
                    this.label1.Text = cj_names[i].ToString();
                    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                    i = i + 1;
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
           this.WindowState = FormWindowState.Maximized;
        }
    }
}

解决方案 »

  1.   

    构造函数里的查询的作用是?
    Timer调用位置?
    SqlDataReader不用关闭?
      

  2.   

     List<Image> lst = new List<Image>();
    private void Form1_Load(object sender, EventArgs e)
            {
                InitializeComponent();
                string sql_str = "select * from cj_main";
                using(SqlConnection conn = new SqlConnection(conn_str))
              {
                SqlCommand command = new SqlCommand(sql_str, conn);
                conn.Open();
                SqlDataReader rs = command.ExecuteReader();
                List<string>names = new List<string>();
                while(rs.Read())
                {
                    cj_names = rs[1].ToString();
                    cj_pic_path = rs[3].ToString();
                    lst.Add(Image.FromFile(cj_pic_path));
                 }
              rs.Close();}
            }
    timer1_Tick中获取随机数
    ran.Next(0,lst.Count-1);
      

  3.   

    rs固定循环2次,而i又是个局全量 当i大的时侯 
    rs循环的第一次 是将i置0
    rs循环的第二次 才是变图。
    而变的图始终都是list[0]
      

  4.   

    每次Tick触发都去数据库里拿一次,何苦啊.
    其他都不想说了.
      

  5.   

    代码写的好复杂哦。在窗体构造的时候就把数据读取出来放在集合或者数组里面,在  timer1_Tick 循环遍历里面的数据数据就可以了,你在timer1_Tick 里面写 查询数据连接数据库 服务交互频繁,容易卡机。
      

  6.   

    你们不知道呀,还一步。当图片的在ENTER事件中出现到给定的次数以后就不在参加图片循环了。所以每次TIMER_TICK都必须要读,字段cj_count值到给定数值后就不在参见SELECT查询了。
      

  7.   

    就是每次都对 cj_counts做-1当数值=0的时候就不循环了.