开门见山,使用MS Enterprise Library的DAAB(Data Access Application Block)获取数据时抛出异常。具体场景如下,通过Database对象的ExecuteReader执行两段Select语句,前一句是不合法的,后一句是正确的。为了避免第一次执行出错导致程序的终止,特意将其放到Try/Catch酷快中。两次数据库操作通过TrsanctionScope的形式纳入同一个Transaction中,具体的代码如下所示。
class Program
2: {
void Main()
4: {
5:
;
;
8:
9:
10: Database db = DatabaseFactory.CreateDatabase();
new TransactionScope())
12: {
13: DbCommand commandWithInvalidSql = db.GetSqlStringCommand(invalidSql);
14: DbCommand commandWithValidSql = db.GetSqlStringCommand(validSql);
15:
try
17: {
18: db.ExecuteReader(commandWithInvalidSql);
19: }
catch
21: { }
22:
23: db.ExecuteReader(commandWithValidSql);
24: }
25: }
26: }