1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
//------------------------------------------------------------------------------
// <copyright file="OleDbException.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data.OleDb {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
[Serializable]
public sealed class OleDbException : System.Data.Common.DbException {
private OleDbErrorCollection oledbErrors;
internal OleDbException(string message, OleDbHResult errorCode, Exception inner) : base(message, inner) {
HResult = (int)errorCode;
this.oledbErrors = new OleDbErrorCollection(null);
}
internal OleDbException(OleDbException previous, Exception inner) : base(previous.Message, inner) {
HResult = previous.ErrorCode;
this.oledbErrors = previous.oledbErrors;
}
private OleDbException(string message, Exception inner, string source, OleDbHResult errorCode, OleDbErrorCollection errors) : base(message, inner) { // MDAC 84364
Debug.Assert(null != errors, "OleDbException without OleDbErrorCollection");
Source = source;
HResult = (int)errorCode;
this.oledbErrors = errors;
}
// runtime will call even if private...
private OleDbException(SerializationInfo si, StreamingContext sc) : base(si, sc) {
oledbErrors = (OleDbErrorCollection) si.GetValue("oledbErrors", typeof(OleDbErrorCollection));
}
[System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
override public void GetObjectData(SerializationInfo si, StreamingContext context) { // MDAC 72003
if (null == si) {
throw new ArgumentNullException("si");
}
si.AddValue("oledbErrors", oledbErrors, typeof(OleDbErrorCollection));
base.GetObjectData(si, context);
}
[
TypeConverterAttribute(typeof(ErrorCodeConverter))
]
override public int ErrorCode {
get {
return base.ErrorCode;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public OleDbErrorCollection Errors {
get {
OleDbErrorCollection errors = this.oledbErrors;
return ((null != errors) ? errors : new OleDbErrorCollection(null));
}
}
internal bool ShouldSerializeErrors() { // MDAC 65548
OleDbErrorCollection errors = this.oledbErrors;
return ((null != errors) && (0 < errors.Count));
}
static internal OleDbException CreateException(UnsafeNativeMethods.IErrorInfo errorInfo, OleDbHResult errorCode, Exception inner) { // MDAC 84364
OleDbErrorCollection errors = new OleDbErrorCollection(errorInfo);
string message = null;
string source = null;
OleDbHResult hr = 0;
if (null != errorInfo) {
hr = errorInfo.GetDescription(out message);
Bid.Trace("<oledb.IErrorInfo.GetDescription|API|OS|RET> %08X{HRESULT}, Description='%ls'\n", hr, message);
hr = errorInfo.GetSource(out source);
Bid.Trace("<oledb.IErrorInfo.GetSource|API|OS|RET> %08X{HRESULT}, Source='%ls'\n", hr, source);
}
int count = errors.Count;
if (0 < errors.Count) {
StringBuilder builder = new StringBuilder();
if ((null != message) && (message != errors[0].Message)) { // WebData 103032
builder.Append(message.TrimEnd(ODB.ErrorTrimCharacters)); // MDAC 73707
if (1 < count) {
builder.Append(Environment.NewLine);
}
}
for (int i = 0; i < count; ++i) {
if (0 < i) {
builder.Append(Environment.NewLine);
}
builder.Append(errors[i].Message.TrimEnd(ODB.ErrorTrimCharacters)); // MDAC 73707
}
message = builder.ToString();
}
if (ADP.IsEmpty(message)) {
message = ODB.NoErrorMessage(errorCode); // MDAC 71170
}
return new OleDbException(message, inner, source, errorCode, errors);
}
static internal OleDbException CombineExceptions(List<OleDbException> exceptions) {
Debug.Assert(0 < exceptions.Count, "missing exceptions");
if (1 < exceptions.Count) {
OleDbErrorCollection errors = new OleDbErrorCollection(null);
StringBuilder builder = new StringBuilder();
foreach(OleDbException exception in exceptions) {
errors.AddRange(exception.Errors);
builder.Append(exception.Message);
builder.Append(Environment.NewLine);
}
return new OleDbException(builder.ToString(), null, exceptions[0].Source, (OleDbHResult)exceptions[0].ErrorCode, errors);
}
else {
return exceptions[0];
}
}
sealed internal class ErrorCodeConverter : Int32Converter { // MDAC 68557
// converter classes should have public ctor
public ErrorCodeConverter() {
}
override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw ADP.ArgumentNull("destinationType");
}
if ((destinationType == typeof(string)) && (value != null) && (value is Int32)) {
return ODB.ELookup((OleDbHResult) value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
}
|