Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Odbc / OdbcException.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="OdbcException.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 using System;
10 using System.ComponentModel;    //Component
11 using System.Collections;       //ICollection
12 using System.Data;
13 using System.Data.Common;
14 using System.Globalization;
15 using System.Runtime.InteropServices;
16 using System.Runtime.Serialization;
17 using System.Text;
18
19 namespace System.Data.Odbc {
20
21     [Serializable]
22     public sealed class OdbcException : System.Data.Common.DbException {
23         OdbcErrorCollection odbcErrors = new OdbcErrorCollection();
24
25         ODBC32.RETCODE _retcode;    // DO NOT REMOVE! only needed for serialization purposes, because Everett had it.
26
27         static internal OdbcException CreateException(OdbcErrorCollection errors, ODBC32.RetCode retcode) {
28             StringBuilder builder = new StringBuilder();
29             foreach (OdbcError error in errors) {
30                 if (builder.Length > 0) {
31                     builder.Append(Environment.NewLine);
32                 }
33
34                 builder.Append(Res.GetString(Res.Odbc_ExceptionMessage, ODBC32.RetcodeToString(retcode), error.SQLState, error.Message)); // MDAC 68337
35             }
36             OdbcException exception = new OdbcException(builder.ToString(), errors);
37             return exception;
38         }
39
40         internal OdbcException(string message, OdbcErrorCollection errors) : base(message) {
41             odbcErrors = errors;
42             HResult = HResults.OdbcException;
43         }
44
45         // runtime will call even if private...
46         private OdbcException(SerializationInfo si, StreamingContext sc) : base(si, sc) {
47             _retcode = (ODBC32.RETCODE) si.GetValue("odbcRetcode", typeof(ODBC32.RETCODE));
48             odbcErrors = (OdbcErrorCollection) si.GetValue("odbcErrors", typeof(OdbcErrorCollection));
49             HResult = HResults.OdbcException;
50         }
51
52         public OdbcErrorCollection Errors {
53             get {
54                 return odbcErrors;
55             }
56         }
57
58         [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
59         override public void GetObjectData(SerializationInfo si, StreamingContext context) {
60             // MDAC 72003
61             if (null == si) {
62                 throw new ArgumentNullException("si");
63             }
64             si.AddValue("odbcRetcode", _retcode, typeof(ODBC32.RETCODE));
65             si.AddValue("odbcErrors", odbcErrors, typeof(OdbcErrorCollection));
66             base.GetObjectData(si, context);
67             }
68
69         // mdac bug 62559 - if we don't have it return nothing (empty string)
70         override public string Source {
71             get {
72                 if (0 < Errors.Count) {
73                     string source = Errors[0].Source;
74                     return ADP.IsEmpty(source) ? "" : source; // base.Source;
75                 }
76                 return ""; // base.Source;
77             }
78         }
79     }
80 }