轻松玩转Typed DataSet, Part III

轻松玩转Typed DataSet, Part III

 

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

posted @ 2004-12-11 02:00  Rickie  阅读(2011)  评论(1编辑  收藏  举报