我想当数据库连接错误的时候,捕获并处理异常。处理是返回当前界面停止下面的所有操作,捕获异常后用messagebox提示出错原因.                    SqlConnection con = new SqlConnection(conn_str);
                    SqlCommand command1 = new SqlCommand(sql_str, con);
                    con.Open();
                    SqlDataReader rs1 = command1.ExecuteReader();
                    try
                    {
                        while (rs1.Read())
                        {
                            if(File.Exists(rs1[3].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)
                    {
                        //忽略路径无效图片
                    }
                    finally
                    {
                        con.Close();
                        rs1.Close();
                    }

解决方案 »

  1.   


                        try
                        {
                        SqlConnection con = new SqlConnection(conn_str);
                        SqlCommand command1 = new SqlCommand(sql_str, con);
                        con.Open();
                        SqlDataReader rs1 = command1.ExecuteReader();
                            while (rs1.Read())
                            {
                                if(File.Exists(rs1[3].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)
                        {
                            //忽略路径无效图片
                        }
                        finally
                        {
                            con.Close();
                            rs1.Close();
                        }
      

  2.   

    提示 ,错误.
    finally                    
    {
          con.Close();
          rs1.Close();                 
    }
    错误 1 当前上下文中不存在名称“con” C:\cj_sys\cj_sys\Form1.cs 90 25 cj_sys
    错误 1 当前上下文中不存在名称“rs1” C:\cj_sys\cj_sys\Form1.cs 90 25 cj_sys而且这样只捕获了文件不存在的异常呀,数据库的异常怎么捕获?
      

  3.   

     catch(Sysetem.Exception ex)
                        {
                            //忽略路径无效图片
                        MessageBox.Show("Error",ex.Message)
                        }
      

  4.   

    用catch(exception ex)这是捕获所有错误,如果只想捕获数据库的错误的话,可以看看都有哪些错误,另外,在你的finally那里,直接写
    con.Close();
    rs1.Close();
    会有问题,一是顺序反了,先关闭rs1,然后再关闭数据库连接con,另外,如果con在open的时候就出错咯,那你close的话,同样也会报告错误,所以你在关闭con之前,要先判断下其状态