1 //------------------------------------------------------------------------------
2 // <copyright file="OleDbError.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 //------------------------------------------------------------------------------
9 namespace System.Data.OleDb {
12 using System.Data.Common;
13 using System.Diagnostics;
14 using System.Globalization;
15 using System.Runtime.InteropServices;
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;
24 internal OleDbError(UnsafeNativeMethods.IErrorRecords errorRecords, int index) {
26 int lcid = System.Globalization.CultureInfo.CurrentCulture.LCID;
28 Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS>\n");
29 UnsafeNativeMethods.IErrorInfo errorInfo = errorRecords.GetErrorInfo(index, lcid);
30 if (null != errorInfo) {
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);
36 if (OleDbHResult.DB_E_NOLOCALE == hr) { // MDAC 87303
37 Bid.Trace("<oledb.ReleaseComObject|API|OS> ErrorInfo\n");
38 Marshal.ReleaseComObject(errorInfo);
40 Bid.Trace("<oledb.Kernel32.GetUserDefaultLCID|API|OS>\n");
41 lcid = SafeNativeMethods.GetUserDefaultLCID();
43 Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS> LCID=%d\n", lcid);
44 errorInfo = errorRecords.GetErrorInfo(index, lcid);
46 if (null != errorInfo) {
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);
53 if ((hr < 0) && ADP.IsEmpty(this.message)) {
54 this.message = ODB.FailedGetDescription(hr);
56 if (null != errorInfo) {
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);
62 if (OleDbHResult.DB_E_NOLOCALE == hr) { // MDAC 87303
63 Marshal.ReleaseComObject(errorInfo);
65 Bid.Trace("<oledb.Kernel32.GetUserDefaultLCID|API|OS>\n");
66 lcid = SafeNativeMethods.GetUserDefaultLCID();
68 Bid.Trace("<oledb.IErrorRecords.GetErrorInfo|API|OS> LCID=%d\n", lcid);
69 errorInfo = errorRecords.GetErrorInfo(index, lcid);
71 if (null != errorInfo) {
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);
78 if ((hr < 0) && ADP.IsEmpty(this.source)) {
79 this.source = ODB.FailedGetSource(hr);
81 Bid.Trace("<oledb.Marshal.ReleaseComObject|API|OS> ErrorInfo\n");
82 Marshal.ReleaseComObject(errorInfo);
86 UnsafeNativeMethods.ISQLErrorInfo sqlErrorInfo;
88 Bid.Trace("<oledb.IErrorRecords.GetCustomErrorObject|API|OS> IID_ISQLErrorInfo\n");
89 hr = errorRecords.GetCustomErrorObject(index, ref ODB.IID_ISQLErrorInfo, out sqlErrorInfo);
91 if (null != sqlErrorInfo) {
93 Bid.Trace("<oledb.ISQLErrorInfo.GetSQLInfo|API|OS>\n");
94 this.nativeError = sqlErrorInfo.GetSQLInfo(out this.sqlState);
96 Bid.Trace("<oledb.ReleaseComObject|API|OS> SQLErrorInfo\n");
97 Marshal.ReleaseComObject(sqlErrorInfo);
101 public string Message {
103 string message = this.message;
104 return ((null != message) ? message : ADP.StrEmpty);
108 public int NativeError {
110 return this.nativeError;
114 public string Source {
116 string source = this.source;
117 return ((null != source) ? source : ADP.StrEmpty);
121 public string SQLState {
123 string sqlState = this.sqlState;
124 return ((null != sqlState) ? sqlState : ADP.StrEmpty);
128 override public string ToString() {