2011-07-05 74 views
0

我有2个类。使用通用数据访问类,我可以从存储过程GetDepartments获取部门。我的问题是,在目录访问类,特别是在公共静态DataTable ExecuteSelectCommand(DbCommand命令)(执行命令并返回结果作为DataTable对象)我不知道我必须写在CATCH循环或如何离开它blank.Can任何人都可以帮我完成这部分?或者,我可以怎样才能改变它没有尝试赶上。带trycatch函数的问题

using System;  
using System.Data;  
using System.Data.Common;  
using System.Configuration;  

public static class GenericDataAccess  
{  
    static GenericDataAccess() 
    { 

    } 


    public static DataTable ExecuteSelectCommand(DbCommand command)  
    {  
    DataTable table; 

    try  
    { 

     command.Connection.Open();  
     DbDataReader reader = command.ExecuteReader();  
     table = new DataTable();   
     table.Load(reader);   
     reader.Close();   
    }  
    catch (...)  
    {  
     ...... 
    }  
    finally  
    {   
     command.Connection.Close();  
    } 

    return table;  
    } 

    public static DbCommand CreateCommand()  
    {  
    string dataProviderName = BalloonShopConfiguration.DbProviderName; 
    string connectionString = BalloonShopConfiguration.DbConnectionString; 

    DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);  
    DbConnection conn = factory.CreateConnection(); 

    conn.ConnectionString = connectionString; 

    DbCommand comm = conn.CreateCommand();  
    comm.CommandType = CommandType.StoredProcedure; 

    return comm; 
    } 
} 

**The Catalog Access class:** 

using System;  
using System.Data;  
using System.Data.Common; 

public static class CatalogAccess  
{  
    static CatalogAccess() 
    { 

    } 

    public static DataTable GetDepartments()  
    {  
    DbCommand comm = GenericDataAccess.CreateCommand(); 

    comm.CommandText = "GetDepartments"; 

    return GenericDataAccess.ExecuteSelectCommand(comm);  
    }  
} 
+0

什么如果ExecuteSelectCommand方法中发生异常,你想这么做吗?这将决定什么需要进入catch块。 – DoctorMick

+1

读者必须在里面使用。 – Aristos

+0

看起来Connection可能同样泄漏。 – Constantin

回答

1

如果你不知道该怎么做或不想处理任何异常,与catch了离开。这是有效的:

try 
{ 
    // code here 
} 
finally 
{ 
    // cleanup here 
} 

..这样,任何异常将传递给调用方法的方法。如果try块中存在问题(例外),则此方法将退出,但不会在执行finally中的任何代码之前退出。

+0

当我尝试第二种方式,你建议我有这个错误'无法打开数据库“BalloonShop”登录请求。登录失败。'。错误是在这行“command.Connection.Open();” – dali1985

+0

@ theo1985,可能是您使用的连接字符串不正确。检查你连接到正确的数据库服务器,该服务器有'BalloonShop'数据库。 – Constantin

0

我不知道要问什么,但我怀疑你不想处理函数中的异常,并让它沿着栈向下传播(即让调用者处理它)。

为了达到这个目的,您可以将catch-clause从代码中删除。代码finally仍然会被调用。如果你想处理您功能异常,但在返回前重新抛出它,尝试:

catch (MyException e) 
{ 
    // Do stuff with e 
    throw; 
} 
+3

你失去了堆栈跟踪信息。好得多只是'扔;' –

+0

@Kieren:thx,编辑我的答案。 – larsmoa

+0

这样做会给你一个关于未使用变量'e'的编译器警告,所以如果你打算这么做的话,就把它扔掉,然后'catch {throw; }'会更好。或者,如果你不介意可读性的轻微损失,那么根本就没有捕获块... –

0

与时处理异常,你有两个选择:你现在可以处理他们时,他们正在抛出,或者你可以让他们冒充代码并在稍后处理。哪个更好取决于你的程序到底做了什么。

你如何处理它也取决于你的代码。你想通知用户吗?重试连接?都?什么也不做(坏!)?让我们说你只是想让用户知道发生了什么坏事。然后,你会做类似如下:

try{ 
    // breakable stuff 
}catch(Exception e){ 
    System.Windows.Forms.MessageBox.Show("Something broke: " + e.Message); 
}finally{ 
    // clean up 
} 

如果你想处理异常进一步向上(又名在调用这个方法),然后执行以下操作:

try{ 
    // breakable stuff 
}catch{ 
    throw; 
}finally{ 
    // clean up 
} 
+0

'catch {throw; }'不会添加任何有价值的东西,除了可能会放置断点的行。但是,它可能应该在'#if DEBUG'部分中。 – Constantin

+0

的确如此,但它也不会伤害任何东西,IMO也会使它更具可读性,并且更清楚地知道究竟发生了什么。 –

+0

抱歉:'(再一次,你当然是正确的,但是(正如你所说的)对于OP的代码来说,删除一个'catch'就像删除所有注释一样有效的性能,以减少编译时间:D –