110,571
社区成员
发帖
与我相关
我的任务
分享
SqlConnection con = new SqlConnection(conn_str);
SqlCommand command1 = new SqlCommand(sql_str, con);
con.Open();
SqlDataReader rs1 = command1.ExecuteReader();
while (rs1.Read())
{
try
{
if (File.Exists(rs1[1].ToString()))
{
cj_names = rs1[1].ToString();
cj_pic_path = rs1[3].ToString();
cj_id = rs1[0].ToString();
names.Add(cj_names);
id.Add(cj_id);
lst.Add(Image.FromFile(cj_pic_path));
//重新从数据库装载LIST图片信息
}
}
catch (FileNotFoundException)
{
//忽略错误地址图片继续装入下一条图片记录
}
con.Close();
}
namespace cj_sys
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
List<Image> lst = new List<Image>();
List<string> names = new List<string>();
List<string> id = new List<string>();
string conn_str = "Server=*;Database=*;uid=sa;pwd=*";
string sql_str = "select * from cj_main where cj_show_counts>0";
string sql_update;
string cj_pic_path;
string cj_names;
string cj_id;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
if(lst.Count>0)
{
timer1.Enabled = true;
}
else
{
MessageBox.Show("奖品数量不足", "抽奖信息出错");
}
//开始抽奖
}
if (e.KeyCode == Keys.Enter)
{
timer1.Enabled = false;
if (lst.Count > 1)
{
string ids = label3.Text;
//确定抽奖结果
sql_update = "update cj_main set cj_show_counts =
cj_show_counts-1 where cj_id='" + Convert.ToInt32(ids) + "'";
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand update = new SqlCommand(sql_update, conn);
conn.Open();
update.ExecuteNonQuery();
conn.Close();
lst.Clear();
names.Clear();
id.Clear();
//更新抽中图片记录的次数
SqlConnection con = new SqlConnection(conn_str);
SqlCommand command1 = new SqlCommand(sql_str, con);
con.Open();
SqlDataReader rs1 = command1.ExecuteReader();
while (rs1.Read())
{
try
{
if (File.Exists(rs1[1].ToString()))
{
cj_names = rs1[1].ToString();
cj_pic_path = rs1[3].ToString();
cj_id = rs1[0].ToString();
names.Add(cj_names);
id.Add(cj_id);
lst.Add(Image.FromFile(cj_pic_path));
//重新从数据库装载LIST图片信息
}
}
catch (FileNotFoundException)
{
//忽略错误地址图片继续装入下一条图片记录
}
con.Close();
}
}
else
{
timer1.Enabled = false;
lst.Clear();
names.Clear();
id.Clear();
MessageBox.Show("奖品数量不足","抽奖信息出错");
//处理剩余奖品数量不足以继续抽奖,终止。
}
}
if (e.KeyData == Keys.Escape)
{
Application.Exit();
//退出程序
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
try
{
while (rs.Read())
{
if (File.Exists(rs[3].ToString()))
{
cj_names = rs[1].ToString();
cj_pic_path = rs[3].ToString();
cj_id = rs[0].ToString();
names.Add(cj_names);
id.Add(cj_id);
lst.Add(Image.FromFile(cj_pic_path));
//从数据库中取得图片信息
}
}
}
catch (FileNotFoundException)
{
//如果找不到图片继续寻找下一条记录
}
conn.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (lst.Count > 1)
{
pictureBox1.Image = lst[i];
label1.Text = names[i];
label3.Text = id[i];
i++;
if (i >= lst.Count)
{
i = 0;
}
//窗体初始装载LIST图片
}
else
{
lst.Clear();
names.Clear();
id.Clear();
timer1.Enabled = false;
MessageBox.Show("奖品数量不足", "抽奖信息出错");
//数据库中已没有符合条件的记录
}
}
}
}
catch (FileNotFoundException)
{
//忽略错误地址图片继续装入下一条图片记录
}
con.Close();
}
catch (FileNotFoundException)
{
//如果找不到图片继续寻找下一条记录
}
conn.Close();
while (rs1.Read())
{
try
{
if (File.Exists(rs1[1].ToString()))
{
cj_names = rs1[1].ToString();
cj_pic_path = rs1[3].ToString();
cj_id = rs1[0].ToString();
names.Add(cj_names);
id.Add(cj_id);
lst.Add(Image.FromFile(cj_pic_path));
//重新从数据库装载LIST图片信息
}
}
catch (FileNotFoundException)
{
//忽略错误地址图片继续装入下一条图片记录
}
con.Close(); //关闭连接不能放在循环里面,这样你最多只能读一条数据,然后就报你所讲的错误
}