Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data / System / Data / OleDb / OleDbError.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="OleDbError.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 //------------------------------------------------------------------------------
8
9 namespace System.Data.OleDb {
10
11     using System;
12     using System.Data.Common;
13     using System.Diagnostics;
14     using System.Globalization;
15     using System.Runtime.InteropServices;
16
17     [Serializable]
18     public sealed class OleDbError {
19         readonly private string message;
20         readonly private string source;
21         readonly private string sqlState;
22         readonly private int nativeError;
23
24         internal OleDbError(UnsafeNativeMethods.IErrorRecords errorRecords, int index) {
25             OleDbHResult hr;
26             int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
27
28             Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS>\n");
29             UnsafeNativeMethods.IErrorInfo errorInfo = errorRecords.GetErrorInfo(index, lcid);
30             if (null != errorInfo) {
31
32                 Bid.Trace("<oledb.IErrorInfo.GetDescription|API|OS>\n");
33                 hr = errorInfo.GetDescription(out this.message);
34                 Bid.Trace("<oledb.IErrorInfo.GetDescription|API|OS|RET> Message='%ls'\n", this.message);
35
36                 if (OleDbHResult.DB_E_NOLOCALE == hr) { // MDAC 87303
37                     Bid.Trace("<oledb.ReleaseComObject|API|OS> ErrorInfo\n");
38                     Marshal.ReleaseComObject(errorInfo);
39
40                     Bid.Trace("<oledb.Kernel32.GetUserDefaultLCID|API|OS>\n");
41                     lcid = SafeNativeMethods.GetUserDefaultLCID();
42
43                     Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS> LCID=%d\n", lcid);
44                     errorInfo = errorRecords.GetErrorInfo(index, lcid);
45
46                     if (null != errorInfo) {
47
48                         Bid.Trace("<oledb.IErrorInfo.GetDescription|API|OS>\n");
49                         hr = errorInfo.GetDescription(out this.message);
50                         Bid.Trace("<oledb.IErrorInfo.GetDescription|API|OS|RET> Message='%ls'\n", this.message);
51                     }
52                 }
53                 if ((hr < 0) && ADP.IsEmpty(this.message)) {
54                     this.message = ODB.FailedGetDescription(hr);
55                 }
56                 if (null != errorInfo) {
57
58                     Bid.Trace("<oledb.IErrorInfo.GetSource|API|OS>\n");
59                     hr = errorInfo.GetSource(out this.source);
60                     Bid.Trace("<oledb.IErrorInfo.GetSource|API|OS|RET> Source='%ls'\n", this.source);
61
62                     if (OleDbHResult.DB_E_NOLOCALE == hr) { // MDAC 87303
63                         Marshal.ReleaseComObject(errorInfo);
64
65                         Bid.Trace("<oledb.Kernel32.GetUserDefaultLCID|API|OS>\n");
66                         lcid = SafeNativeMethods.GetUserDefaultLCID();
67
68                         Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS> LCID=%d\n", lcid);
69                         errorInfo = errorRecords.GetErrorInfo(index, lcid);
70
71                         if (null != errorInfo) {
72
73                             Bid.Trace("<oledb.IErrorInfo.GetSource|API|OS>\n");
74                             hr = errorInfo.GetSource(out this.source);
75                             Bid.Trace("<oledb.IErrorInfo.GetSource|API|OS|RET> Source='%ls'\n", this.source);
76                         }
77                     }
78                     if ((hr < 0) && ADP.IsEmpty(this.source)) {
79                         this.source = ODB.FailedGetSource(hr);
80                     }
81                     Bid.Trace("<oledb.Marshal.ReleaseComObject|API|OS> ErrorInfo\n");
82                     Marshal.ReleaseComObject(errorInfo);
83                 }
84             }
85
86             UnsafeNativeMethods.ISQLErrorInfo sqlErrorInfo;
87
88             Bid.Trace("<oledb.IErrorRecords.GetCustomErrorObject|API|OS> IID_ISQLErrorInfo\n");
89             hr = errorRecords.GetCustomErrorObject(index, ref ODB.IID_ISQLErrorInfo, out sqlErrorInfo);
90
91             if (null != sqlErrorInfo) {
92
93                 Bid.Trace("<oledb.ISQLErrorInfo.GetSQLInfo|API|OS>\n");
94                 this.nativeError = sqlErrorInfo.GetSQLInfo(out this.sqlState);
95
96                 Bid.Trace("<oledb.ReleaseComObject|API|OS> SQLErrorInfo\n");
97                 Marshal.ReleaseComObject(sqlErrorInfo);
98             }
99         }
100
101         public string Message {
102             get {
103                 string message = this.message;
104                 return ((null != message) ? message : ADP.StrEmpty);
105             }
106         }
107
108         public int NativeError {
109             get {
110                 return this.nativeError;
111             }
112         }
113
114         public string Source {
115             get {
116                 string source = this.source;
117                 return ((null != source) ? source : ADP.StrEmpty);
118             }
119         }
120
121         public string SQLState {
122             get {
123                 string sqlState = this.sqlState;
124                 return ((null != sqlState) ? sqlState : ADP.StrEmpty);
125             }
126         }
127
128         override public string ToString() {
129             return Message;
130         }
131     }
132 }