2 // System.Data.SqlClient.SqlException.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
10 // Copyright (C) Tim Coleman, 2002
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using Mono.Data.Tds.Protocol;
38 using System.ComponentModel;
40 using System.Data.Common;
41 using System.Runtime.Serialization;
44 namespace System.Data.SqlClient {
45 [SerializableAttribute()]
47 public sealed class SqlException
56 private SqlErrorCollection errors;
57 private const string DEF_MESSAGE = "SQL Exception has occured.";
63 internal SqlException ()
64 : this (DEF_MESSAGE, null, null)
68 internal SqlException (string message, Exception inner)
69 : this (message, inner, null)
73 internal SqlException (string message, Exception inner, SqlError sqlError)
74 : base (message == null ? DEF_MESSAGE : message, inner)
76 errors = new SqlErrorCollection ();
78 errors.Add (sqlError);
81 internal SqlException (byte theClass, int lineNumber, string message, int number, string procedure, string server, string source, byte state)
84 new SqlError (theClass, lineNumber, message,
85 number, procedure, server, source,
90 private SqlException(SerializationInfo si, StreamingContext sc)
92 errors = (SqlErrorCollection)si.GetValue("Errors", typeof(SqlErrorCollection));
95 #endregion // Constructors
100 get { return Errors [0].Class; }
103 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
104 public SqlErrorCollection Errors {
105 get { return errors; }
108 public int LineNumber {
109 get { return Errors [0].LineNumber; }
112 public override string Message {
114 if (Errors.Count == 0)
116 StringBuilder result = new StringBuilder ();
117 if (base.Message != DEF_MESSAGE) {
118 result.Append (base.Message);
119 result.Append ("\n");
121 for (int i=0; i < Errors.Count -1; i++) {
122 result.Append (Errors [i].Message);
123 result.Append ("\n");
125 if (Errors.Count > 0)
126 result.Append (Errors [Errors.Count - 1].Message);
127 return result.ToString ();
132 get { return Errors [0].Number; }
135 public string Procedure {
136 get { return Errors [0].Procedure; }
139 public string Server {
140 get { return Errors [0].Server; }
143 public override string Source {
144 get { return Errors [0].Source; }
148 get { return Errors [0].State; }
151 #endregion // Properties
156 internal static SqlException FromTdsInternalException (TdsInternalException e)
158 return FromTdsInternalException (null, e);
161 internal static SqlException FromTdsInternalException (string message,
162 TdsInternalException e)
164 SqlError sqlError = new SqlError (e.Class, e.LineNumber, e.Message,
165 e.Number, e.Procedure, e.Server,
166 "Mono SqlClient Data Provider", e.State);
167 return new SqlException (message, e, sqlError);
170 public override void GetObjectData (SerializationInfo si, StreamingContext context)
173 throw new ArgumentNullException ("si");
175 si.AddValue ("Errors", errors, typeof(SqlErrorCollection));
176 base.GetObjectData (si, context);
179 #endregion // Methods