如果数据库里的图片不存在,图片路径变量用:cj_pic_path。 我想要忽略这个错误直接抽取下一个图片如何处理能不报错?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=PC360;Database=choujiang;uid=sa;pwd=dsd_dsdn";
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);
try
{
conn.Open();
update.ExecuteNonQuery();
throw new Exception();
} catch (Exception)
{
Console.WriteLine("数据库连接错误更新失败!");
}
finally
{
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())
{
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图片信息
}
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();
while (rs.Read())
{
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));
//从数据库中取得图片信息
}
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("奖品数量不足", "抽奖信息出错");
//数据库中已没有符合条件的记录
} } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
} }
}
{
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=PC360;Database=choujiang;uid=sa;pwd=dsd_dsdn";
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);
try
{
conn.Open();
update.ExecuteNonQuery();
throw new Exception();
} catch (Exception)
{
Console.WriteLine("数据库连接错误更新失败!");
}
finally
{
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())
{
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图片信息
}
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();
while (rs.Read())
{
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));
//从数据库中取得图片信息
}
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("奖品数量不足", "抽奖信息出错");
//数据库中已没有符合条件的记录
} } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
} }
}
{
if(NoFileFound)
continue;
else
//load file
}