求大家帮完善这段代码...

bulls5988 2010-11-04 11:18:30
如果数据库里的图片不存在,图片路径变量用: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();
}

}
}

...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bulls5988 2010-11-04
  • 打赏
  • 举报
回复
这个我会的 我的问题是怎么忽略这个错误继续向下装载图片
旅行者I号 2010-11-04
  • 打赏
  • 举报
回复
system.io.File.Exists (cj_pic_path)检查该路径表示的文件是否存在
旅行者I号 2010-11-04
  • 打赏
  • 举报
回复
难道是这个意思,不了个是吧

while(ooxx)
{
if(NoFileFound)
continue;
else
//load file
}

vpoint2011 2010-11-04
  • 打赏
  • 举报
回复
GOTO~~~~~~~~~~~~~~~~~~~~~~~~`~`

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧