轻松玩转Typed DataSet, Part III

轻松玩转Typed DataSet, Part III<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

Written by: Rickie Lee

Dec. 10, 2004

 

本文继续前面《轻松玩转Typed DataSet, Part II》,这里演练在使用Typed DataSet过程中,如何有效地Debug程序中出现的错误。最常见的错误Exception应该是:

An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll

 

Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

 

显然,通过VS.Net IDE显式的上述Exception信息,很难判断到底错误在什么地方。这里提供的一个有效的方法来Debug,尽快找到Bug的真正原因,并加以解决。

private void PrintAllErrs(DataSet myDataSet)

{

  DataRow[] rowsInError;

 

  foreach(DataTable myTable in myDataSet.Tables)

  {

            // Test if the table has errors. If not, skip it.

            if(myTable.HasErrors)

            {

                        // Get an array of all rows with errors.

                        rowsInError = myTable.GetErrors();

                        Console.WriteLine(myTable.TableName + " " + rowsInError.Length.ToString()

                                    + " " + rowsInError[0].RowError);

                        // Print the error of each column in each row.

                        /*for(int i = 0; i < rowsInError.Length; i++)

                        {

                                    foreach(DataColumn myCol in myTable.Columns)

                                    {

                                                Console.WriteLine(myCol.ColumnName + " " +

                                                            rowsInError[i].GetColumnError(myCol));

                                    }

                                    // Clear the row errors

                                    rowsInError[i].ClearErrors();

                        }*/

            }

  }

}

 

通过上面的PrintAllErrs()方法,明确输出错误的信息,其中注释的for语句根据时间情况来确定是否需要。

下面演示如何使用上述Code snippet,下面的代码与前面的post轻松玩转Typed DataSet, Part II》相关,更详细的信息,请参考《轻松玩转Typed DataSet, Part II》:

AnnotationTypedDataset theOrderDS = new AnnotationTypedDataset();

try

{

            string strSelectOrders = "Select top 5 * From Orders ";

            strSelectOrders += "Select * From [Order Details]";

            SqlHelper.FillDataset(connStr, CommandType.Text, strSelectOrders, theOrderDS, new string[] {"Orders", "OrderDetails"});

           

            StringBuilder strResults = new StringBuilder();

           

            foreach(AnnotationTypedDataset.Order theOrder in theOrderDS.Orders)

            {

                        strResults.Append(theOrder.OrderID.ToString() + " "

                                    + theOrder.CustomerID.ToString() + " "

                                    + theOrder.EmployeeID.ToString() + Environment.NewLine);

                        strResults.Append("Order Details: ");

                        strResults.Append(theOrder.GetChildRows("OrdertoOrderDetails").Length.ToString() + " ");

                        strResults.Append(theOrder.GetOrderDetails().Length.ToString());

                        strResults.Append(Environment.NewLine);

            }

           

            txtResults.Text = strResults.ToString();

}

catch

{

            PrintAllErrs(theOrderDS);

}

 

异常Exception输出结果:

OrderDetails 2141 ForeignKeyConstraint OrdertoOrderDetails requires the child key values (10253) to exist in the parent table.

 

包含有错误的table name,错误的Rows总数,并输出第1行错误的具体信息,如果要输出所有行的错误信息,则需要开发PrintAllErrs()方法的for循环。

通过上面输出的错误信息,可以很快发现是上面的SQL语句有问题,子表OrderDetailskey值在父表Orders不存在。

 

Any questions or errors, please leave comments below. Thanks.

 

References:

1. Rickie, 轻松玩转Typed DataSet, Part I

2. Rickie, 轻松玩转Typed DataSet, Part II

3. MSDN, DataTable.GetErrors Method, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataTableClassGetErrorsTopic.asp

转载于:https://www.cnblogs.com/rickie/archive/2004/12/11/75473.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值