檢視具體datatable的錯誤資訊
阿新 • • 發佈:2018-12-31
遇到這個錯誤 Failed to enable constraints. One or more rows contain values violatingnon-null, unique, or foreign-key constraints。
它檢查DataSet中的所有DataTable的HasErrors屬性,然後對有錯誤的DataTable使用GetErrors方法。
GetErrors返回一組DataRows,檢查返回的所有DataRow的RowError屬性來獲得確切的錯誤資訊。
解決方法:
DataTable dataTable=new DataTable(); try { SqlDataAdapter.Fill(dataTable); //這行會出錯,先catch住 } catch { //remove datatable errors DataRow[] rowsInError; if (dataTable.HasErrors) { // Get an array of all rows with errors. rowsInError = dataTable.GetErrors(); // Print the error of each column in each row. StringBuilder sbError = new StringBuilder(); for (int i = 0; i < rowsInError.Length; i++) { foreach (DataColumn myCol in dataTable.Columns) { sbError.Append(myCol.ColumnName + " " + rowsInError[i].GetColumnError(myCol)); } // Clear the row errors rowsInError[i].ClearErrors(); //設定斷點,然後執行時觀察其中錯誤 } } }