DB2 9 应用开辟(733 测验)认证指南,第 6 局部: .NET 编程(5)

完成 DB2 与 .NET 框架的无缝接口
developerWorks








毛病检讨

在本节中,您将学习若何应用 ADO.NET 应用顺序的毛病检讨器材。 您将窥察应用 Exception 器材和 DB2 .NET 跟踪及 DB2 跟踪实用顺序举行底子的错误处理惩罚和呈文。

错误处理惩罚

如果在 ADO.NET 中实行准确的错误处理惩罚要领,在 ADO.NET 应用顺序中,应该将逻辑包括在 try/catch 块中。当 ADO.NET 要领中泛起错误时,将抛出 Exception 器材。因而, 对付包括数据库操作的每个 try 块,响应的 catch 块应该包括处理惩罚一个 Exception 器材的逻辑。

在清单 10 中的示例中,一个 SELECT 语句中的 WHERE 子句含有标准不兼容的操作数:STAFF 表的 JOB 列的数据标准是 VARCHAR,而不是 INTEGER

清单 10. try catch 块

                    
try
{
  ...
  cmd.Transaction = trans; 
  cmd.CommandText = "SELECT ID, NAME FROM STAFF WHERE JOB = 99";
  reader = cmd.ExecuteReader();    
  ...
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}


try 块中泛起一个非常时,catch 块将捕获该非常。

ex.Message 包括非常的内容。含不兼容操作数的 SELECT 语句会生成以下非常:

ERROR [42818] [IBM][DB2/NT] SQL0401N  The data types of the operands for the 
operation "=" are not compatible.  SQLSTATE=42818







回页首



DB2 ADO.NET 跟踪

为了调试 DB2 ADO.NET 应用顺序,可以应用 DB2 ADO.NET 跟踪实用顺序。 要激活 ADO.NET 跟踪实用顺序,可以设置以下环境变量:

SET DB2NMPTRACE=1


激活 ADO.NET 跟踪实用顺序之后,每当运转一个 DB2 ADO.NET 应用顺序,就会生成对 ADO.NET 挪用的一个具体的跟踪。清单 11 展示了后面开辟的 AccessSamplDb 应用顺序生成的跟踪数据:

清单 11. .NET 跟踪示例

                    
| DB2Trace.TraceVersion fnc data 10: Version: 9.0.0.1
| DB2Trace.TraceVersion fnc data 11: Framework: 1.1.4322.2300
| DB2Interop.Init fnc entry
| | DB2Interop.Init fnc data 10: file:///c:/windows/assembly/gac/ibm.data.db2/9.0.0.1
__7c307b91aa13d208/ibm.data.db2.dll
| | DB2Interop.Init fnc data 19: 9.1.0.DEF.1
| | DB2Interop.Init fnc data 20: DB2COPY1
| | DB2Interop.Init fnc data 30: D:\IBM\SQLLIB\
| | DB2Interop.Init fnc data 50: D:\IBM\SQLLIB\\bin\db2app.dll
| | DB2Interop.Init fnc data 55: a
| | DB2Interop.Init fnc data 60: 15
| | DB2Interop.Init fnc data 97: SOFTWARE\IBM\DB2\InstalledCopies
| | DB2Interop.Init fnc data 98: DB2COPY1
| | DB2Interop.Init fnc data 99: 0
| DB2Interop.Init fnc exit2, rc = 0 - False, 15
db2trace False
| DB2ConnPool.GetDB2ConnPool fnc data 5: 0
| DB2ConnPool.GetDB2ConnPool fnc data 10: 1
| DB2ConnPool.GetDB2ConnPool fnc data 11: 55706604
| DB2ConnPool.GetDB2ConnPool fnc data 254: 1 1
DB2ConnPool.GetDB2ConnPool fnc exit, rc = 0
DB2ConnPool.DB2ConnPool fnc exit, rc = 0
DB2Connection.DB2Connection1 fnc entry
| DBCWrapper.DBCWrapper fnc entry
| DBCWrapper.DBCWrapper fnc exit, rc = 0
| DB2Connection.ConnectionString.set fnc entry1 - Database=SAMPLE
| | DB2Connection.State.get fnc entry
| | | DB2Connection.State.get api data 10: Closed
| | DB2Connection.State.get fnc exit, rc = 0
| | DB2ConnPool.ReplaceConnectionStringParms fnc entry
| | | DB2ConnPool.ReplaceConnectionStringParms fnc data 10: Database=SAMPLE
| | | DB2ConnPool.ReplaceConnectionStringParms fnc data 15: database, SAMPLE
| | | DB2ConnPool.ReplaceConnectionStringParms fnc data 20: dsn=SAMPLE;
| | DB2ConnPool.ReplaceConnectionStringParms fnc exit, rc = 0
| DB2Connection.ConnectionString.set fnc exit, rc = 0
DB2Connection.DB2Connection1 fnc exit, rc = 0
DB2Connection.Open fnc entry
| DB2Connection.State.get fnc entry
| | DB2Connection.State.get api data 10: Closed
| DB2Connection.State.get fnc exit, rc = 0
| DB2ConnPool.GetDB2ConnPool fnc entry
| | DB2ConnPool.GetDB2ConnPool fnc data 254: 1 2
| DB2ConnPool.GetDB2ConnPool fnc exit, rc = 0


当具有含不兼容操作数的 SELECT 语句的顺序运转时, .NET 跟踪实用顺序捕获到错误:

清单 12. .NET 跟踪实用顺序捕获到的错误

                    
DB2Command.ExecuteReader2 fnc exit, rc = 0
DB2ConnPool.CheckClose fnc entry
DB2Exception.Message.get fnc entry
| DB2ConnPool.CheckClose fnc data 10: 2/8/2007 4:14:39 PM
| | DB2Resource.DB2Resource fnc entry2 - db2nmp.xml, null
| | DB2ConnPoolWrapper.LockPool fnc entry
| | | DB2Resource.GetMsgPath fnc entry
| | | | DB2ConnPoolWrapper.LockPool fnc data 10: dsn=SAMPLE;
| | | | | DB2Resource.GetMsgPath fnc data 10: D:\IBM\SQLLIB\
| | | | DB2ConnPoolWrapper.LockPool fnc exit, rc = 0
| | | DB2Resource.GetMsgPath fnc exit1, rc = 0 - D:\IBM\SQLLIB\msg\en_US
| | | DB2ConnPool.CheckClose fnc data 20:    dsn=SAMPLE;, xa: False, conns: 1, free:
0
| | DB2Resource.DB2Resource fnc exit, rc = 0
| | DB2ConnPool.CheckClose fnc data 25: POOL [dsn=SAMPLE;] 1->NULL    pPool.pLastConn
 = 1
| | DB2Exception.Message.get fnc data 20: ERROR [42818] [IBM][DB2/NT] SQL0401N  The d
ata types of the operands for the operation "=" are not compatible.  SQLSTATE=42818
| | DB2ConnPool.CheckClose fnc data 30:       dbc: 1, xa: False, inuse: True, free on
 close: False, lifetime: -1, 0
| DB2Exception.Message.get fnc exit, rc = 0
| DB2ConnPoolWrapper.UnlockPool fnc entry
ERROR [42818] [IBM][DB2/NT] SQL0401N  The data types of the operands for the operatio
n "=" are not compatible.  SQLSTATE=42818


DB2 .NET Provider 应用 DB2 CLI 的一些组件。这对付 DB2 ADO.NET 应用顺序确定成就无意会很有效,可以获得 CLI 和 DB2 跟踪信息。




版权声明: 原创作品,许愿转载,转载时请务必以超链接情势标明文章 原始因由 、作者信息和本声明。不然将清查司法责任。

原文地址:https://www.cnblogs.com/zgqjymx/p/1972834.html