I have 2 classes. With the Generic Data Access class I can get the departments from the stored procedure GetDepartments. My problem is that in the Catalog Access class and especially in the public static DataTable ExecuteSelectCommand(DbCommand co开发者_StackOverflow社区mmand)(execute a command and returns the results as a DataTable object) I dont know what I must write in the CATCH loop or how to leave it blank.Can anyone please help me to complete this part?Or maybe how can i change it without Try-catch.
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);
}
}
If you don't know what to do or don't want to handle any exceptions, leave with catch
out. This is valid:
try
{
// code here
}
finally
{
// cleanup here
}
..that way, any exceptions will be passed up to the method that called your method. If there is a problem (exception) in the try
block, the method will exit, but not before any code in finally
is executed.
When dealing with exceptions, you have two options: you can handle them now when they're thrown, or you can let them bubble up the code and handle them later. Which is better depends on what exactly your program does.
How you handle it also depends on your code. Do you want to inform the user? Retry the connection? Both? Do nothing (bad!)? Lets say you just want to let the user know something bad happened. Then you'd do something like the following:
try{
// breakable stuff
}catch(Exception e){
System.Windows.Forms.MessageBox.Show("Something broke: " + e.Message);
}finally{
// clean up
}
If you want to deal with the exception further up (aka in the method that called this one), then do the following:
try{
// breakable stuff
}catch{
throw;
}finally{
// clean up
}
I'm not sure what to ask for, but I suspect that you want to don't handle the exception in your function and let it be propogated down the stack (i.e. let the caller handle it).
To achieve that you could just leave the catch
-clause out of your code. The code below finally
will still be called. If you wish to handle the exception in your function, but rethrow it before returning, try:
catch (MyException e)
{
// Do stuff with e
throw;
}
精彩评论