代码之家  ›  专栏  ›  技术社区  ›  odiseh

.NET:在开发CRUD操作时,您在try-catch块的“catch”部分中的自信方法是什么?

  •  0
  • odiseh  · 技术社区  · 15 年前

    我想知道在.NET中开发CRUD操作(特别是当您使用数据库作为数据源时)时,在try catch块的catch部分是否有任何可靠的方法可以使用?

    嗯,你对下面几行有什么看法?

    public int Insert(string name, Int32 employeeID, string createDate)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = this._ConnectionString;
            try
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "UnitInsert";
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                SqlCommandBuilder.DeriveParameters(command);
                command.Parameters["@Name"].Value = name;
                command.Parameters["@EmployeeID"].Value = employeeID;
                command.Parameters["@CreateDate"].Value = createDate;
                int i = command.ExecuteNonQuery();
                command.Dispose();
                return i;
            }
            catch
            {
                **// how do you "catch" any possible error here?**
                return 0;
                //
            }
            finally
            {
                connection.Close();
                connection.Dispose();
                connection = null;
            }
        }
    
    4 回复  |  直到 15 年前
        1
  •  3
  •   kemiller2002    15 年前

    首先,我将使用using语句。

    我不会作为失败返回0。您可以成功地更新没有记录,因此0将是有效的成功响应代码。使用-1显然表明出了问题。就我个人而言,我更愿意在预期的事情发生时抛出一个例外。

    try
        { 
           using (SqlConnection connection = new SqlConnection())
            {
    
                connection.Open();         
    
                using(SqlCommand command = connection.CreateCommand())
                {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "UnitInsert";
    
                        SqlCommandBuilder.DeriveParameters(command);
                        command.Parameters["@Name"].Value = name;
                        command.Parameters["@EmployeeID"].Value = employeeID;
                        command.Parameters["@CreateDate"].Value = createDate;
    
                        return command.ExecuteNonQuery();
                   }
    
            }
            }
            catch(Exception ex)
            {
              LogException(ex);
               return either -1 or re throw exception.
            }
    
        2
  •  2
  •   Paddy    15 年前

    在我看来,这完全是一个错误的地方,捕捉任何你不能处理的东西,然后。让异常冒泡起来,让调用应用程序实现它自己的错误处理。如果您在这里捕获并接受一个异常,那么您对已安装应用程序的调试将是一场噩梦。

    只有抓住你能处理的,然后扔掉其他的东西…

        3
  •  0
  •   derek    15 年前

    我会做一个

    catch(Exception ex){
         // log exception here and set return value
    }
    
        4
  •  0
  •   Allon Guralnek    15 年前

    我喜欢通过将所有异常收缩到选定的几个异常中,并将实际异常嵌套为 InnerException .

    例如,所有不是调用者错误的数据库异常都将引发一种类型的异常,所有属于调用者错误的数据库异常(例如未选择行、无效的pk、无效的数据)将引发另一种类型的异常(或者甚至可能在两种异常之间有更细微的差别)n个异常类型),以及与数据库无关的最后一个异常类型(健全性检查、nullRef等),但我无法处理的异常除外(例如 OutOfMemoryException )

    这样就很容易捕捉到我在DAL中抛出的异常,所有具体的血腥细节仍然可以在 神经感觉