trycatch 函数的问题
我有2节课。使用通用数据访问类,我可以从存储过程 GetDepartments 获取部门。我的问题是,在 Catalog Access 类中,尤其是在公共静态 DataTable ExecuteSelectCommand(DbCommand command)(执行命令并将结果作为 DataTable 对象返回) 中,我不知道必须在 CATCH 循环中写入什么或如何离开它空白。任何人都可以帮我完成这部分吗?或者也许我如何在没有 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);
}
}
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 command)(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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不知道该怎么做或者不想处理任何异常,请使用
catch
退出。这是有效的:..这样,任何异常都将传递到调用您的方法的方法。如果
try
块中出现问题(异常),该方法将退出,但不会在执行finally
中的任何代码之前退出。If you don't know what to do or don't want to handle any exceptions, leave with
catch
out. This is valid:..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 infinally
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:
If you want to deal with the exception further up (aka in the method that called this one), then do the following:
我不确定要要求什么,但我怀疑您不想处理函数中的异常并让它沿着堆栈传播(即让调用者处理它)。
要实现这一点,您只需将
catch
子句从代码中删除即可。finally
下面的代码仍然会被调用。如果您希望在函数中处理异常,但在返回之前重新抛出异常,请尝试: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 belowfinally
will still be called. If you wish to handle the exception in your function, but rethrow it before returning, try: